How can I specify the C compiler to use in a Cargo config file?

Currently I can build a project with

CC=/opt/muslcc/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-gcc cargo build --target=armv7-unknown-linux-musleabihf

However, I'd like to not have to pass the CC environment variable each time.

I tried this:

~/bzip_mre> cat .cargo/config.toml
[target.armv7-unknown-linux-musleabihf]
linker = "rust-lld"
rustc-flags = "-L CC=/opt/muslcc/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-gcc"

as per 1 with the result:

~/bzip_mre> cargo build --target=armv7-unknown-linux-musleabihf
error: expected a table, but found a string for `target.armv7-unknown-linux-musleabihf.rustc-flags` in /home/neumann/bzip_mre/.cargo/config.toml

How do I correctly move this environment variable into the cargo config?

I also tried

~/bzip_mre> cat .cargo/config.toml
[target.armv7-unknown-linux-musleabihf]
linker = "rust-lld"
rustc-flags = "-L /opt/muslcc/arm-linux-musleabihf-cross/bin"

Resulting in the same error message.

Also tried

[target.armv7-unknown-linux-musleabihf]
linker = "rust-lld"
rustc-env = { CC = "/opt/muslcc/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-gcc" }

With

~/bzip_mre> cargo build --target=armv7-unknown-linux-musleabihf                                                                             2024-11-20T11:04:40
   Compiling bzip2-sys v0.1.11+1.0.8
The following warnings were emitted during compilation:

warning: bzip2-sys@0.1.11+1.0.8: Compiler family detection failed due to error: ToolNotFound: Failed to find tool. Is `arm-linux-musleabihf-gcc` installed?

error: failed to run custom build command for `bzip2-sys v0.1.11+1.0.8`

Caused by:
  process didn't exit successfully: `/home/neumann/bzip_mre/target/debug/build/bzip2-sys-e61bed0673a19778/build-script-build` (exit status: 1)
  --- stdout
  OPT_LEVEL = Some(0)
  TARGET = Some(armv7-unknown-linux-musleabihf)
  HOST = Some(x86_64-unknown-linux-gnu)
  cargo:rerun-if-env-changed=CC_armv7-unknown-linux-musleabihf
  CC_armv7-unknown-linux-musleabihf = None
  cargo:rerun-if-env-changed=CC_armv7_unknown_linux_musleabihf
  CC_armv7_unknown_linux_musleabihf = None
  cargo:rerun-if-env-changed=TARGET_CC
  TARGET_CC = None
  cargo:rerun-if-env-changed=CC
  CC = None
  cargo:rerun-if-env-changed=CROSS_COMPILE
  CROSS_COMPILE = None
  RUSTC_LINKER = Some(rust-lld)
  cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
  cargo:warning=Compiler family detection failed due to error: ToolNotFound: Failed to find tool. Is `arm-linux-musleabihf-gcc` installed?
  RUSTC_WRAPPER = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some(true)
  CARGO_CFG_TARGET_FEATURE = None
  cargo:rerun-if-env-changed=CFLAGS_armv7-unknown-linux-musleabihf
  CFLAGS_armv7-unknown-linux-musleabihf = None
  cargo:rerun-if-env-changed=CFLAGS_armv7_unknown_linux_musleabihf
  CFLAGS_armv7_unknown_linux_musleabihf = None
  cargo:rerun-if-env-changed=TARGET_CFLAGS
  TARGET_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None

  --- stderr


  error occurred: Failed to find tool. Is `arm-linux-musleabihf-gcc` installed?

I.e. the CC flag appears to be ignored.

Try using the [env] section in your configuration file. CC (or in your case setting CC_armv7-unknown-linux-musleabihf would probably be better) is read by the cc crate and has nothing to do with the rustc-flags, which are CLI options passed to rustc invocations by Cargo.

3 Likes