Skip to content

Setting rustflags does not work with --target #9743

@nmattia

Description

@nmattia

Problem

It looks like cargo ignores the rustflags set by environment variables (RUSTFLAGS and CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS and CARGO_BUILD_RUSTFLAGS) when given --target. For instance, in a build linked against -liconv, cargo build succeeds when given the proper linker arguments, whereas cargo build --target <my actual target> doesn't:

$ printenv CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS
-C link-arg=-L/path/to/iconv/lib
$ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
$ cargo build --target x86_64-apple-darwin
   Compiling unicode-xid v0.2.1
   Compiling proc-macro2 v1.0.26
   Compiling syn v1.0.69
   Compiling autocfg v1.0.1
   Compiling serde v1.0.125
   Compiling serde_derive v1.0.125
   Compiling typenum v1.13.0
   Compiling version_check v0.9.3
   Compiling memchr v2.3.4
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "-arch" "x86_64" <thousands of args that do NOT include -L/path/to/iconv/lib> "-lSystem" "-lresolv" "-lc" "-lm" "-liconv"
  = note: ld: library not found for -liconv
          clang-7: error: linker command failed with exit code 1 (use -v to see invocation)

If this is the expected behavior, how do I set the linker flags?

Notes

From the Compilation Options page, I understand that this "makes Cargo run in a different mode where the target artifacts are placed in a separate directory". However, as I understand it, this should only impact where the target artifacts are placed, not how the configuration is read.

Output of cargo version:

$ cargo version
cargo 1.53.0 (4369396ce 2021-04-27)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions