Skip to content

In package with binary and library targets, binary can't link to native libs #8372

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
mbrubeck opened this issue Jun 16, 2020 · 1 comment
Closed
Labels
C-bug Category: bug

Comments

@mbrubeck
Copy link
Contributor

mbrubeck commented Jun 16, 2020

When a Cargo package contains both a binary crate and a library crate, linker arguments from the build script are used only when building the library crate.

Originally reported at: https://users.rust-lang.org/t/cant-compile-gtk-hello-world-app/44404

Steps to reproduce:

On a system with zlib installed, run cargo new empty to create a new binary project. Add a build.rs file with the following contents:

fn main() { println!("cargo:rustc-link-lib=z"); }

Replace src/main.rs with the following code:

extern "C" { pub fn zlibVersion() -> *const i8; }
fn main() { unsafe { zlibVersion(); } }

This builds successfully. Note that Cargo passes -l z to rustc when compiling src/main.rs:

$ cargo build -v
   Compiling empty v0.1.0 (/Users/mattbrubeck/src/test/empty)
     Running `rustc --crate-name empty --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=e0168590142cc8f4 -C extra-filename=-e0168590142cc8f4 --out-dir /Users/mattbrubeck/src/test/empty/target/debug/deps -C incremental=/Users/mattbrubeck/src/test/empty/target/debug/incremental -L dependency=/Users/mattbrubeck/src/test/empty/target/debug/deps -l z`
    Finished dev [unoptimized + debuginfo] target(s) in 0.19s

Next, run touch src/lib.rs to create an empty library crate. Then build again.

This fails with a linker error when linking the binary. Note that Cargo passes -l z to rustc when compiling src/lib.rs but not when compiling src/main.rs:

$ touch src/lib.rs
$ cargo build -v
   Compiling empty v0.1.0 (/Users/mattbrubeck/src/test/empty)
     Running `/Users/mattbrubeck/src/test/empty/target/debug/build/empty-e48820509d59e846/build-script-build`
     Running `rustc --crate-name empty --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=74a0fcb7e53d7be8 -C extra-filename=-74a0fcb7e53d7be8 --out-dir /Users/mattbrubeck/src/test/empty/target/debug/deps -C incremental=/Users/mattbrubeck/src/test/empty/target/debug/incremental -L dependency=/Users/mattbrubeck/src/test/empty/target/debug/deps -l z`
     Running `rustc --crate-name empty --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=8f7bc0d71f260462 -C extra-filename=-8f7bc0d71f260462 --out-dir /Users/mattbrubeck/src/test/empty/target/debug/deps -C incremental=/Users/mattbrubeck/src/test/empty/target/debug/incremental -L dependency=/Users/mattbrubeck/src/test/empty/target/debug/deps --extern empty=/Users/mattbrubeck/src/test/empty/target/debug/deps/libempty-74a0fcb7e53d7be8.rlib`
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-L" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/mattbrubeck/src/test/empty/target/debug/deps/empty-8f7bc0d71f260462.14fgmq3pyd8wbdzy.rcgu.o" "/Users/mattbrubeck/src/test/empty/target/debug/deps/empty-8f7bc0d71f260462.1nmjqwqdeg3fnvtr.rcgu.o" "/Users/mattbrubeck/src/test/empty/target/debug/deps/empty-8f7bc0d71f260462.222cmglc6zpl1h9c.rcgu.o" "/Users/mattbrubeck/src/test/empty/target/debug/deps/empty-8f7bc0d71f260462.47vtrnft28odh9ni.rcgu.o" "/Users/mattbrubeck/src/test/empty/target/debug/deps/empty-8f7bc0d71f260462.4bedw166csntaxsa.rcgu.o" "-o" "/Users/mattbrubeck/src/test/empty/target/debug/deps/empty-8f7bc0d71f260462" "/Users/mattbrubeck/src/test/empty/target/debug/deps/empty-8f7bc0d71f260462.57a1c5asbd3w922o.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/mattbrubeck/src/test/empty/target/debug/deps" "-L" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-02be9dff15a2e744.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-a0dbc9da17125acb.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-be16c55789af999a.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-7323f6a9a844a3dc.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace-9de488196a5a1e00.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace_sys-8b6a39e8ef70be7d.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-a4569d4d3725ef76.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-7d1ef8925268e123.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-709a2b0cfa362a8d.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-98194019dd82bdcf.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-38feed196e113296.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-37c2c6822f6e4416.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-2e6e530c778a3583.rlib" "/Users/mattbrubeck/.rustup/toolchains/nightly-2020-05-09-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-0d81316630962f56.rlib" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: Undefined symbols for architecture x86_64:
            "_zlibVersion", referenced from:
                empty::main::h4f36ec3104a21c20 in empty-8f7bc0d71f260462.1nmjqwqdeg3fnvtr.rcgu.o
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: aborting due to previous error

error: could not compile `empty`.

Caused by:
  process didn't exit successfully: `rustc --crate-name empty --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=8f7bc0d71f260462 -C extra-filename=-8f7bc0d71f260462 --out-dir /Users/mattbrubeck/src/test/empty/target/debug/deps -C incremental=/Users/mattbrubeck/src/test/empty/target/debug/incremental -L dependency=/Users/mattbrubeck/src/test/empty/target/debug/deps --extern empty=/Users/mattbrubeck/src/test/empty/target/debug/deps/libempty-74a0fcb7e53d7be8.rlib` (exit code: 1)

Proposed solution

Cargo should pass linker arguments from the build script to rustc for all targets of the package.

@mbrubeck mbrubeck added the C-bug Category: bug label Jun 16, 2020
@mbrubeck
Copy link
Contributor Author

Oops, this is a duplicate of #7506.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

1 participant