Skip to content

rustflags in config.toml not being passed to linker in Linux but work fine in windows. #141566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ajundi opened this issue May 26, 2025 · 2 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@ajundi
Copy link

ajundi commented May 26, 2025

linker is not using rustflags defined in the config.toml file in LINUX only (it is working fine in windows with same code and rustc version).
I ran the same project in linux and windows and displayed the linker arguments using cargo rustc -- --print=link-args my config.toml inside project's .cargo folder contains the following.

[target.'cfg(all(target_arch = "arm", target_os = "none"))']
rustflags = ["-C", "link-arg=-Tlink.x"]

[build]
target = "thumbv7em-none-eabihf"

In windows I got the following linker args at the end of the linker command
... target\\thumbv7em-none-eabihf\\debug\\deps\\i2c-4754388f49bb4437" "--gc-sections" "-Tlink.x"

In linux this is what I get at the end of my linker args

... target/thumbv7em-none-eabihf/debug/deps/i2c-ce1445d509cca9ef" "--gc-sections"

WORKAROUND:
Manually assigning RUSTFLAGS before running the build command works correctly
RUSTFLAGS="-C link-arg=-Tlink.x" cargo rustc -- --print=link-args

This displayed the correct linker arguments in linux and probe-rs worked great.
......target/thumbv7em-none-eabihf/debug/deps/i2c-9061183c020b57e6" "--gc-sections" "-Tlink.x"

rustc --version --verbose:

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

I also tried the nightly version same issue.

rustc 1.89.0-nightly (5e16c6620 2025-05-24)
binary: rustc
commit-hash: 5e16c662062fd6dee91f0fe2a1580483488d80cf
commit-date: 2025-05-24
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5
@ajundi ajundi added the C-bug Category: This is a bug. label May 26, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 26, 2025
@bjorn3
Copy link
Member

bjorn3 commented May 26, 2025

On your Linux system did you have RUSTFLAGS set? If so that overrides the rustflags in .cargo/config.toml. And were you inside the directory containing .cargo/config.toml when you ran the build command.

@ajundi
Copy link
Author

ajundi commented May 26, 2025

On your Linux system did you have RUSTFLAGS set? If so that overrides the rustflags in .cargo/config.toml. And were you inside the directory containing .cargo/config.toml when you ran the build command.

Ohhhhh it seems to be set but empty in my env variables sorry about that. I tried echoing it before but nothing showed up. That explains it.

Just in case you still want the information

I ran the command from multiple folders (I initially used older version of these projects where config.toml did have the correct linker arg -Tlink.x) however the latest version referenced below was also tried and though it might be easier for you to try out:

1- From discovery-mb2 main folder using cargo build -p led-roulette then checking the elf file readelf -l ./target/thumbv7em-none-eabihf/debug/led-roulette
2- I also tried running it in the project folder containing .cargo folder and ` folder . /discovery-mb2/mdbook/src/08-led-roulette

https://github.com/rust-embedded/discovery-mb2 commit 1621aab5ed325708524aa63c94e2e75b7fac298e on main (latest)

@ajundi ajundi closed this as completed May 26, 2025
@jieyouxu jieyouxu added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

4 participants