Is there a way to get the default linker flags from rustc?


#1

It gets useful to generate working pkg-conf files and other build system integration.


#2

rustc -Z print-link-args foo.rs returns the flags that rustc passes to the linker to link the foo executable.

Is that what you wanted?


#3

yes and no, the output is :

"cc" "-m64" "-L" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "foo.0.o" "-o" "foo" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-411f48d3.rlib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-411f48d3.rlib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-411f48d3.rlib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librand-411f48d3.rlib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcollections-411f48d3.rlib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_unicode-411f48d3.rlib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-411f48d3.rlib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc_jemalloc-411f48d3.rlib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-411f48d3.rlib" "/Users/lu_zero/.multirust/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-411f48d3.rlib" "-l" "System" "-l" "pthread" "-l" "c" "-l" "m" "-l" "compiler-rt"

I really need the -lSystem -lpthread -lc -lm -lcompiler-rt line.


#4

Parsing the output of -Z print-link-args for -l flags seems easier but there is another way: You can scan the source code of the crate and its dependencies and look for #[link(name = "foo")] lines in *.rs files and for println!("cargo:rustc-link-lib=bar") in build.rs files; there foo and bar are the arguments to -l that will be passed to the linker.

This information about which © libraries a crate links to may also be available in a .rlib metadata. But (a) I’m not sure how to extract that information from the metadata (running the crate through strings may be a good start tough) and (b) the metadata format is not stable.

So, yeah there’s no direct way to get the flags you want and I think I have already listed all the existing ways to indirectly get that information. You could request a flag for this exact purpose via an RFC issue though. Just be sure to clearly include your use case.

Is there a way to get the default linker flags from rustc?
default

Hmm, yet another way could be to simply keep (i.e. hardcode) a map from (rustc version + target) to the flags, if you only ever want the -l flags that are used to build the simplest program that depends on std.