Linking to c library fails in devcontainer environment #8035

I am trying to test a rust application linking with a C based shared lib built by . Both C library and rust being compliled in devcontainer environment (Ubuntu 20.04 LTS based).
build.rs has required changes to link to the lib and specify the path. While same things works well when build in my linux dev machine, 'cargo build' fails when tested in devcontainer running on same dev machine. Error:
error: linking with cc failed: exit status: 1
...
= note: /usr/bin/ld: /work/rust-app/target/debug/deps/rust_app-f390bc1401f0732c.3zy2ogu46grmljay.rcgu.o: in function rust_app::main': /work/rust-app/src/main.rs:14: undefined reference to from_the_library'
collect2: error: ld returned 1 exit status

= note: some extern functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the -l flag to specify native libraries to link
= note: use the cargo:rustc-link-lib directive to specify the native libraries to link with Cargo (see Build Scripts - The Cargo Book)

error: could not compile rust-app (bin "rust-app") due to previous error

build.rs::
fn main() {
println!("cargo:rustc-link-lib=rtest");
println!("cargo:rustc-link-search=/home/vmohare/rust/rust-conan/libs");
}

Build log:

aos-dev ➜ /work/rust-app $ cargo -vv build
Compiling rust-app v0.1.0 (/work/rust-app)
Running CARGO=/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_CRATE_NAME=build_script_build CARGO_MANIFEST_DIR=/work/rust-app CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=rust-app CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/work/rust-app/target/debug/deps:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' /usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name build_script_build --edition=2021 build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=125 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C metadata=c0449ab9b74ff73e -C extra-filename=-c0449ab9b74ff73e --out-dir /work/rust-app/target/debug/build/rust-app-c0449ab9b74ff73e -C incremental=/work/rust-app/target/debug/incremental -L dependency=/work/rust-app/target/debug/deps

 Running `CARGO=/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_CFG_PANIC=unwind CARGO_CFG_TARGET_ARCH=x86_64 CARGO_CFG_TARGET_ENDIAN=little CARGO_CFG_TARGET_ENV=gnu CARGO_CFG_TARGET_FAMILY=unix CARGO_CFG_TARGET_FEATURE=fxsr,sse,sse2 CARGO_CFG_TARGET_HAS_ATOMIC=16,32,64,8,ptr CARGO_CFG_TARGET_OS=linux CARGO_CFG_TARGET_POINTER_WIDTH=64 CARGO_CFG_TARGET_VENDOR=unknown CARGO_CFG_UNIX='' CARGO_ENCODED_RUSTFLAGS='' CARGO_MANIFEST_DIR=/work/rust-app CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=rust-app CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' DEBUG=true HOST=x86_64-unknown-linux-gnu LD_LIBRARY_PATH='/work/rust-app/target/debug/deps:/work/rust-app/target/debug:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' NUM_JOBS=6 OPT_LEVEL=0 OUT_DIR=/work/rust-app/target/debug/build/rust-app-837743bdba014670/out PROFILE=debug RUSTC=/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc RUSTDOC=/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustdoc TARGET=x86_64-unknown-linux-gnu /work/rust-app/target/debug/build/rust-app-c0449ab9b74ff73e/build-script-build`

[rust-app 0.1.0] cargo:rustc-link-lib=rtest
[rust-app 0.1.0] cargo:rustc-link-search=/work/rust-app/rtest-libs
Running CARGO=/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_CRATE_NAME=rust_app CARGO_MANIFEST_DIR=/work/rust-app CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=rust-app CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/work/rust-app/target/debug/deps:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' OUT_DIR=/work/rust-app/target/debug/build/rust-app-837743bdba014670/out /usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name rust_app --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=125 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=8109d3d04ac582d1 -C extra-filename=-8109d3d04ac582d1 --out-dir /work/rust-app/target/debug/deps -C incremental=/work/rust-app/target/debug/incremental -L dependency=/work/rust-app/target/debug/deps -L /work/rust-app/rtest-libs -l rtest
Running CARGO=/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_BIN_NAME=rust-app CARGO_CRATE_NAME=rust_app CARGO_MANIFEST_DIR=/work/rust-app CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=rust-app CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/work/rust-app/target/debug/deps:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' OUT_DIR=/work/rust-app/target/debug/build/rust-app-837743bdba014670/out /usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name rust_app --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=125 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=9488eaea0b458ea9 -C extra-filename=-9488eaea0b458ea9 --out-dir /work/rust-app/target/debug/deps -C incremental=/work/rust-app/target/debug/incremental -L dependency=/work/rust-app/target/debug/deps --extern rust_app=/work/rust-app/target/debug/deps/librust_app-8109d3d04ac582d1.rlib -L /work/rust-app/rtest-libs
error: linking with cc failed: exit status: 1
|
= note: LC_ALL="C" PATH="/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/usr/local/cargo/bin:/usr/local/go/bin:/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/aos-dev/.local/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustc4G7WA9/symbols.o" "/work/rust-app/target/debug/deps/rust_app-9488eaea0b458ea9.1riq4yrwwxif0li4.rcgu.o" "/work/rust-app/target/debug/deps/rust_app-9488eaea0b458ea9.1sm6s1qan7x62fff.rcgu.o" "/work/rust-app/target/debug/deps/rust_app-9488eaea0b458ea9.20tn1c923vtt7vi3.rcgu.o" "/work/rust-app/target/debug/deps/rust_app-9488eaea0b458ea9.3zxc4hfu29xx39wx.rcgu.o" "/work/rust-app/target/debug/deps/rust_app-9488eaea0b458ea9.414x8911lvqgmfcf.rcgu.o" "/work/rust-app/target/debug/deps/rust_app-9488eaea0b458ea9.5dnbf4x42rqgtr1p.rcgu.o" "/work/rust-app/target/debug/deps/rust_app-9488eaea0b458ea9.48wfowyb2ytghfka.rcgu.o" "-Wl,--as-needed" "-L" "/work/rust-app/target/debug/deps" "-L" "/work/rust-app/rtest-libs" "-L" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-90f6ddbf82de36ec.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-480c41e9d4e1f677.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-db744c0ca03eed1d.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-8ba652dc7d4b285a.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-d5f4520a9ebc0d58.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-4ad84de58f0cb463.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-0d10aae2e0f38735.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-0c17cff739e6745b.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-b29e17139dde1aa8.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-1f975299829cc7bd.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-86bae3bc3079f89b.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-321b82c30dffdf5f.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-3dd8a6810a0bdfef.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-eb39a61c0c879984.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-aa769569f91c3548.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-510a192a50a983ed.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-0577018320f99037.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-193cf992125ccd4c.rlib" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-8e138eaf26ebb4a8.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/work/rust-app/target/debug/deps/rust_app-9488eaea0b458ea9" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
= note: /usr/bin/ld: /work/rust-app/target/debug/deps/rust_app-9488eaea0b458ea9.1riq4yrwwxif0li4.rcgu.o: in function rust_app::main': /work/rust-app/src/main.rs:14: undefined reference to from_the_library'
collect2: error: ld returned 1 exit status

= note: some extern functions couldn't be found; some native libraries may need to be installed or have their path specified
= note: use the -l flag to specify native libraries to link
= note: use the cargo:rustc-link-lib directive to specify the native libraries to link with Cargo (see Build Scripts - The Cargo Book)

error: could not compile rust-app (bin "rust-app") due to previous error

Caused by:
process didn't exit successfully: CARGO=/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_BIN_NAME=rust-app CARGO_CRATE_NAME=rust_app CARGO_MANIFEST_DIR=/work/rust-app CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=rust-app CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/work/rust-app/target/debug/deps:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' OUT_DIR=/work/rust-app/target/debug/build/rust-app-837743bdba014670/out /usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name rust_app --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=125 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=9488eaea0b458ea9 -C extra-filename=-9488eaea0b458ea9 --out-dir /work/rust-app/target/debug/deps -C incremental=/work/rust-app/target/debug/incremental -L dependency=/work/rust-app/target/debug/deps --extern rust_app=/work/rust-app/target/debug/deps/librust_app-8109d3d04ac582d1.rlib -L /work/rust-app/rtest-libs (exit status: 1)
aos-dev ➜ /work/rust-app $

Adding the line #[link(name="rtest")] to my main.rs file helped here.
main.rs:
#[link(name="rtest")]
extern "C" {
pub fn from_the_library(a: u8, b:u8) -> u8;
}
build.rs:
fn main() {
println!("cargo:rustc-link-lib=rtest");
println!("cargo:rustc-link-search=/work/rust-app/rtest-libs");
}

So, wondering why would lib linking from build.rs line not work?

Update: This was due to having both library (lib.rs) and binary (main.rs) in same crate, causing the issue. After removing lib.rs, issue got resolved.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.