Cargo - How to set '-C debuginfo=0' in debug mode


I am building rust sources with 'cargo build' command which sets the '-C debuginfo=2' for all crates by default. I want that to be disabled (in debug mode). I tried changing it in the Cargo.toml and various places in code 'src/cargo/core/' & 'src/cargo/core/compiler/' also tried adding it in the toml file of a single crate but this flag is not changing (always sets to 2). When compiled in release mode the opt-level=3 is getting set and the debuginfo flag is not passed to cargo at all.

Is there any way I can disable this flag in debug mode? Let me know if any additional info needed.

To answer the question in the title: Try adding

debug = 0

to Cargo.toml of the crate you want to compile this way.

I’m not sure I completely follow your description in your question text; I’ll re-read a few more times to see if I can make more sense of it (edit: still not making 100% sense to me, as I don’t know what these src/cargo/… paths refer to, but I hope I’m giving a useful answer nonetheless; but feel free to clarify if it doesn’t work).

I haven’t tested it (edit: actually tested it now, too), but controlling the debuginfo level used for the debug-mode built with cargo build should work as described above, according to here and here.


I am giving the reproduction steps here, that makes you get a clarity.

  1. wget
  2. tar -xf rustc-1.69.0-src.tar.xz
  3. cd rustc-1.69.0-src/src/tools/cargo
  4. cargo build -v

(If you get any build error related to openssl, in .../rustc-1.69.0-src/src/tools/cargo/Cargo.toml update openssl dependencies as openssl = { version = '0.10.11', features = ["vendored"] } )

In the console log you see the options for compiling crates => ... --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 ....

I've tried these options given in here and here.. also the below but didn't work

debug = 0

There are 2 toml files. One inside rustc-1.69.0-src/src/tools/cargo/Cargo.toml and the other in rustc-1.69.0-src/Cargo.toml. The rustc-1.69.0-src/Cargo.toml file change worked.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.