Skip to content
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

rustc-link-arg wrong order #12833

Closed
ghost opened this issue Oct 17, 2023 · 5 comments
Closed

rustc-link-arg wrong order #12833

ghost opened this issue Oct 17, 2023 · 5 comments
Labels
C-bug Category: bug S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.

Comments

@ghost
Copy link

ghost commented Oct 17, 2023

Problem

I'm trying to link to a static library with an absolute path using build scripts and rustc-link-arg, but I found that its additional arguments are appended to the end instead of in the middle This is different from rustc-link-lib.

CARGO=/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/bin/cargo CARGO_CRATE_NAME=webp_encoder CARGO_MANIFEST_DIR=/home/circleci/project CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='libwebp bindings' CARGO_PKG_HOMEPAGE='https://crates.io/crates/webp_encoder' CARGO_PKG_LICENSE=MIT CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=webp_encoder CARGO_PKG_README=README CARGO_PKG_REPOSITORY='https://github.com/zzwxh/webp_encoder' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.6.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=6 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/circleci/project/target/debug/deps:/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib:/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib' OUT_DIR=/home/circleci/project/target/debug/build/webp_encoder-0c3cef5dbd1f28c0/out /home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/bin/rustc --crate-name webp_encoder --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --test -C metadata=c4c034865c8a8c44 -C extra-filename=-c4c034865c8a8c44 --out-dir /home/circleci/project/target/debug/deps -C incremental=/home/circleci/project/target/debug/incremental -L dependency=/home/circleci/project/target/debug/deps --extern anyhow=/home/circleci/project/target/debug/deps/libanyhow-a37ff87074167c9e.rlib -C link-arg=/home/circleci/project/lib/aarch64_linux/libwebp.a -C link-arg=/home/circleci/project/lib/aarch64_linux/libsharpyuv.a`
error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/bin:/home/circleci/.cargo/bin:/home/circleci/bin:/home/circleci/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" VSLANG="1033" "cc" "/tmp/rustcn2eHDg/symbols.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.112o565stcsx42k3.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.1402l4488wnfwz7p.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.14a0vgkshv24t96a.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.1df8bj1iwhnbxiya.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.1gclg6ud69kz7r9l.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.1i9n3vfzrfafx7n7.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.1yyves5ncxaxpkyo.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.243dij62b8xkjpv6.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.2hjhdw30iaiygg26.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.2o4uz89asyvh1sbu.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.2q2ipx714ns09xri.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.2sfvofsvl3241egi.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.2xz866bts4ib9upk.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.35y0n1fz63q8tczx.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.39anztfvym4kgo8.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.3be1czdt70qr2bsy.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.3hlaw4a8jjs20bsq.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.3i6txbjf01e5l7lu.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.3iqtshl28lr0pohm.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.3lm7chbphff68930.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.3whybj2iuj7lg0rx.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.3xxx9iul6hggsq89.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.41dob2gpsfkfso90.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.48d1vk8lcw2ni6kw.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.4ay89d2m98eerpl2.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.4b9gaj2u8gfr8gue.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.4ciypgjjw3u9xai5.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.4n1c8i8xc94a3wbf.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.4rgbslz13lbhevqp.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.4szyq1b19rv9uu5y.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.50et8fa0pneyne3v.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.52l3awz6prln2x6z.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.5cd2297fll9y9h39.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.5f9vt4tgg2weyjnk.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.6e98exmdfdai8ru.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.6hrtabypo4xcq78.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.6tp528t6jvyek2w.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.7y3qw53hmvpdurc.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.evbnqyi6x2dryc0.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.icicj590tpqdkqs.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.ulvhf58hkcbjge.rcgu.o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44.1pwmjjgamne63hb8.rcgu.o" "-Wl,--as-needed" "-L" "/home/circleci/project/target/debug/deps" "-L" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libtest-4a0f7947e972e52d.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libgetopts-fe53f665c7592316.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libunicode_width-58c2b5e1ca5d5575.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/librustc_std_workspace_std-ec19369c5bbef562.rlib" "/home/circleci/project/target/debug/deps/libanyhow-a37ff87074167c9e.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd-1af56df70101340f.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libpanic_unwind-2d201e9ec773e2b2.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libobject-11edaa83ea3f42ca.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libmemchr-be5d62f450379333.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libaddr2line-48892539e9a2c4de.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libgimli-f8eb1df1397eacbf.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/librustc_demangle-2e09868052a8b2d5.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd_detect-a7e259ed513c618c.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libhashbrown-791329d15b754d4d.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-00676b12b0ca4248.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libminiz_oxide-77a22a58722b5fb2.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libadler-e9ab6f5f15790e88.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libunwind-fab82cfa2048cd45.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcfg_if-21f6d61e1c83d48b.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/liblibc-bac7f74953c2c715.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/liballoc-9b674ebb751cb2fa.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/librustc_std_workspace_core-088fae249a1081ae.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcore-29f211c7751ad68b.rlib" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcompiler_builtins-26783adfa77949da.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/circleci/.rustup/toolchains/1.73.0-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib" "-o" "/home/circleci/project/target/debug/deps/webp_encoder-c4c034865c8a8c44" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "/home/circleci/project/lib/aarch64_linux/libwebp.a" "/home/circleci/project/lib/aarch64_linux/libsharpyuv.a"
  = note: /usr/bin/ld: /home/circleci/project/lib/aarch64_linux/libwebp.a(picture_csp_enc.o): in function `ImportYUVAFromRGBA':
          picture_csp_enc.c:(.text+0x1cdc): undefined reference to `pow'
          /usr/bin/ld: picture_csp_enc.c:(.text+0x1d34): undefined reference to `pow'
          /usr/bin/ld: /home/circleci/project/lib/aarch64_linux/libwebp.a(webp_enc.o): in function `WebPEncode':
          webp_enc.c:(.text+0x84c): undefined reference to `log10'
          /usr/bin/ld: webp_enc.c:(.text+0x884): undefined reference to `log10'
          /usr/bin/ld: webp_enc.c:(.text+0x8b4): undefined reference to `log10'
          /usr/bin/ld: webp_enc.c:(.text+0x8e8): undefined reference to `log10'
          /usr/bin/ld: webp_enc.c:(.text+0x918): undefined reference to `log10'
          /usr/bin/ld: /home/circleci/project/lib/aarch64_linux/libwebp.a(frame_enc.o):frame_enc.c:(.text+0x1714): more undefined references to `log10' follow
          /usr/bin/ld: /home/circleci/project/lib/aarch64_linux/libwebp.a(quant_enc.o): in function `VP8SetSegmentParams':
          quant_enc.c:(.text+0x2798): undefined reference to `pow'
          /usr/bin/ld: quant_enc.c:(.text+0x27d8): undefined reference to `pow'
          /usr/bin/ld: quant_enc.c:(.text+0x2818): undefined reference to `pow'
          /usr/bin/ld: /home/circleci/project/lib/aarch64_linux/libwebp.a(lossless_enc.o): in function `FastSLog2Slow_C':
          lossless_enc.c:(.text+0x13c8): undefined reference to `log'
          /usr/bin/ld: /home/circleci/project/lib/aarch64_linux/libwebp.a(lossless_enc.o): in function `FastLog2Slow_C':
          lossless_enc.c:(.text+0x1470): undefined reference to `log'
          /usr/bin/ld: /home/circleci/project/lib/aarch64_linux/libsharpyuv.a(sharpyuv_gamma.o): in function `SharpYuvInitGammaTables':
          sharpyuv_gamma.c:(.text+0x88): undefined reference to `pow'
          /usr/bin/ld: sharpyuv_gamma.c:(.text+0x11c): undefined reference to `pow'
          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 https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `webp_encoder` (lib test) due to previous error

In this example, the pow and log10 function exist in libm, but due to the issue, the linker is unable to resolve it.

Steps

No response

Possible Solution(s)

No response

Notes

No response

Version

No response

@ghost ghost added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Oct 17, 2023
@weihanglo
Copy link
Member

I can look into the log and guess how it failed. However, to avoid misunderstanding, could you provide a minimal reproducer if possible, or at least a part of your build script related to linker args?

@weihanglo weihanglo added S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request. and removed S-triage Status: This issue is waiting on initial triage. labels Oct 17, 2023
@ghost
Copy link
Author

ghost commented Oct 17, 2023

commands:

clang -c test.c
ar crv libtest.a test.o
cargo run

test.c:

#include <math.h> // This links to libm

double test_pow(double x, double y) {
    return pow(x,y);
}

build script:

use std::{env, path::PathBuf};

fn main() {
    // Won't compile:
    let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    println!("cargo:rustc-link-arg={}", manifest.join("libtest.a").display());

    // Will compile:
    // let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    // println!("cargo:rustc-link-lib=static=test");
    // println!("cargo:rustc-link-search={}",manifest.display());
}

main.rs:

fn main() {
    unsafe {
        let result = test_pow(2.0, 2.0);
        println!("2^2={}", result);
    }
}

extern "C" {
    fn test_pow(x: f64, y: f64) -> f64;
}

link error:

LC_ALL="C" PATH="/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/root/.vscode-server/bin/f1b07bd25dfad64b0167beb15359ae573aecd2cc/bin/remote-cli:/root/.cargo/bin:/root/.local/bin:/root/bin:/usr/share/Modules/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustcSHqNGI/symbols.o" "/root/code/minimal/target/debug/deps/minimal-d03d5d681f5a51ea.1hvbnfr1jy5zofa1.rcgu.o" "/root/code/minimal/target/debug/deps/minimal-d03d5d681f5a51ea.225pt77pqbp0tbas.rcgu.o" "/root/code/minimal/target/debug/deps/minimal-d03d5d681f5a51ea.2uz0pdvo9swd7e21.rcgu.o" "/root/code/minimal/target/debug/deps/minimal-d03d5d681f5a51ea.4dbmrkve9u9anz3c.rcgu.o" "/root/code/minimal/target/debug/deps/minimal-d03d5d681f5a51ea.4kc5hef30jejk8x.rcgu.o" "/root/code/minimal/target/debug/deps/minimal-d03d5d681f5a51ea.hc8vrf1cjlf0dt9.rcgu.o" "/root/code/minimal/target/debug/deps/minimal-d03d5d681f5a51ea.25hh53sj8pek5rlo.rcgu.o" "-Wl,--as-needed" "-L" "/root/code/minimal/target/debug/deps" "-L" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-6498d8891e016dca.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-3debdee1a9058d84.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-8339c5bd5cbc92bf.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-160ebcebb54c11ba.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-95c75789f1b65e37.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-7e8094f2d6258832.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-bac9783ef1b45db0.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-a1cd87df2f2d8e76.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-7fd06d468d7dba16.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-5ac19487656e05bf.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-c7c35d32cf825c11.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-c523f1571362e70b.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-85f17c92b770a911.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-598d3ba148dadcea.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-a58ec2dab545caa4.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-f9dda8cca149f0fc.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-7ba4c315dd7a3503.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-5ac2993e19124966.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-df2fb7f50dec519a.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/root/code/minimal/target/debug/deps/minimal-d03d5d681f5a51ea" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "/root/code/minimal/libtest.a"
...
"-Wl,-Bdynamic"
 "-lgcc_s" 
"-lutil"
 "-lrt" 
"-lpthread" 
"-lm" // <------------------ links to libm
 "-ldl"
 "-lc"
...
 "-Wl,--gc-sections" 
"-pie" 
"-Wl,-z,relro,-z,now" 
"-nodefaultlibs" 
"/root/code/minimal/libtest.a" // <------------------ libtest.a is at the very end 

= note: /usr/bin/ld: /root/code/minimal/libtest.a(test.o): in function `test_pow':
          test.c:(.text+0x1d): undefined reference to `pow'
          collect2: error: ld returned 1 exit status

@ghost
Copy link
Author

ghost commented Oct 18, 2023

@weihanglo

@weihanglo
Copy link
Member

Hi. Thanks for the example.

cargo:rustc-link-arg under the hood passed in -C link-arg= to rustc. there order link args is out of Cargo's control. From where I can tell, rustc appends -C link-arg options at the very end of the linker invocation. This is for overriding options passed in earlier. See rust-lang/rust#99427. Note that this behavior is not guaranteed but IMO is unlikely changed.

I can see what you did with cargo:rustc-link-lib and cargo:rustc-link-search, which is pretty much the way people doing for linking your lib. You can also add a line cargo:rustc-link-arg=-lm" right after the previuos cargo:rustc-link-arg=<yourlib>. It may link to the right lib you want, though not ideal.

Close as Cargo has no control over how rustc manages the order of linker arguments for -C link-arg.

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale Oct 18, 2023
@ghost
Copy link
Author

ghost commented Oct 18, 2023

I can see what you did with cargo:rustc-link-lib and cargo:rustc-link-search, which is pretty much the way people doing for linking your lib.

Although it's unrelated to this issue, I'd like to know your opinion on the "-l" parameter, specifically why we have search paths and link names. Why don't we default to linking to absolute paths?
This could lead to naming conflicts, and I don't want to change the file name of the static library.
@weihanglo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.
Projects
None yet
Development

No branches or pull requests

1 participant