< 返回版块

Neutron3529 发表于 2020-11-30 01:53

在作死的时候(作死打开-lto),我不能正常编译gmp-mpfr-sys

(未测试关掉lto会怎么样,毕竟编译这玩意要好久)

cargo run --release --verbose检查,发现:

rustc --crate-name fermat --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C opt-level=3 -C panic=abort -C lto -C codegen-units=1 -C metadata=c23e76141d72d005 -C extra-filename=-c23e76141d72d005 --out-dir /me/fermat/target/release/deps -L dependency=/me/fermat/target/release/deps --extern bigint=/me/fermat/target/release/deps/libbigint-ab706995e0461ab2.rlib --extern num_bigint=/me/fermat/target/release/deps/libnum_bigint-e58c3b89843e0a6b.rlib --extern ramp=/me/fermat/target/release/deps/libramp-1ec3a2ae2a63bea3.rlib --extern rug=/me/fermat/target/release/deps/librug-e4c2e85ed5e6ee36.rlib --extern gmp=/me/fermat/target/release/deps/libgmp-b99a780b50358d93.rlib -C target-cpu=native -L native=/me/fermat/target/release/build/gmp-mpfr-sys-f485bf457c722ab9/out/lib

这个命令出了问题

经测试,如果在命令末尾加入-lgmp,则程序可以正常运行

想问一下,要怎样修改程序(build.rs或者Cargo.toml),才能保证执行cargo run --release的时候,rustc在执行时候自动加上-lgmp呢?

(上次本想试试rust+pytorch,似乎也是遇到了同样的问题,不确定这是我设置出了BUG还是别的原因)

(我的CFLAGS直接设置成了-O2 -march=native -pipe -flto,或许不应该写-flto的)


在网上搜索了一遍,学会了dirty fix:

$ cat build.rs
fn main(){
    println!("cargo:rustc-flags=-lgmp")
}
$ cargo run --release
# 成功运行

评论区

写评论
作者 Neutron3529 2020-12-01 00:30

我试了一下,把build.rs删掉,发现这样似乎不行

$ LDFLAGS=-lgmp cargo run --release -- 1000000 0
   Compiling fermat v0.1.0 (/me/fermat)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/home/neutron/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/me/fermat/target/release/deps/fermat-3cb799d3cc0cf333.fermat.4egfbnwy-cgu.0.rcgu.o" "-o" "/me/fermat/target/release/deps/fermat-3cb799d3cc0cf333" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/me/fermat/target/release/deps" "-L" "/me/fermat/target/release/build/gmp-mpfr-sys-f485bf457c722ab9/out/lib" "-L" "/home/neutron/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/tmp/rustcldRJtH/libgmp_mpfr_sys-70d62b1ba20797c6.rlib" "-Wl,--start-group" "-Wl,--end-group" "/home/neutron/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-6e3d052afeb9f711.rlib" "-Wl,-Bdynamic" "-lc" "-lm" "-lrt" "-lpthread" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc"
  = note: /usr/bin/ld: /me/fermat/target/release/deps/fermat-3cb799d3cc0cf333.fermat.4egfbnwy-cgu.0.rcgu.o: in function `rug::integer::big::Integer::pow_mod_mut':
          fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer3big7Integer11pow_mod_mut17ha07f7fa798241a52E+0x6d): undefined reference to `__gmpz_gcdext'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer3big7Integer11pow_mod_mut17ha07f7fa798241a52E+0xac): undefined reference to `__gmpz_clear'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer3big7Integer11pow_mod_mut17ha07f7fa798241a52E+0xc6): undefined reference to `__gmpz_clear'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer3big7Integer11pow_mod_mut17ha07f7fa798241a52E+0xd3): undefined reference to `__gmpz_clear'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer3big7Integer11pow_mod_mut17ha07f7fa798241a52E+0x127): undefined reference to `__gmpz_set'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer3big7Integer11pow_mod_mut17ha07f7fa798241a52E+0x155): undefined reference to `__gmpz_add'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer3big7Integer11pow_mod_mut17ha07f7fa798241a52E+0x16e): undefined reference to `__gmpz_sub'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer3big7Integer11pow_mod_mut17ha07f7fa798241a52E+0x1a8): undefined reference to `__gmpz_powm'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer3big7Integer11pow_mod_mut17ha07f7fa798241a52E+0x1b9): undefined reference to `__gmpz_clear'
          /usr/bin/ld: /me/fermat/target/release/deps/fermat-3cb799d3cc0cf333.fermat.4egfbnwy-cgu.0.rcgu.o: in function `fermat::main':
          fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0x720): undefined reference to `__gmpz_init2'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0x767): undefined reference to `__gmpz_add_ui'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0x897): undefined reference to `__gmpz_init'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0x8ff): undefined reference to `__gmpz_probab_prime_p'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0x931): undefined reference to `__gmpz_add_ui'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0x956): undefined reference to `__gmpz_scan1'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0x98d): undefined reference to `__gmpz_fdiv_q_2exp'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0x9c1): undefined reference to `__gmpz_realloc'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0x9f1): undefined reference to `__gmpz_realloc'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0xa5e): undefined reference to `__gmpz_realloc'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0xa7d): undefined reference to `__gmpz_scan1'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0xab0): undefined reference to `__gmpz_fdiv_q_2exp'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0xaed): undefined reference to `__gmpz_cmp'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0xb14): undefined reference to `__gmpz_init2'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0xb3f): undefined reference to `__gmpz_clear'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0xb4b): undefined reference to `__gmpz_cmp'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN6fermat4main17h94e5928fe136289cE+0xd48): undefined reference to `__gmpz_clear'
          /usr/bin/ld: /me/fermat/target/release/deps/fermat-3cb799d3cc0cf333.fermat.4egfbnwy-cgu.0.rcgu.o: in function `rug::integer::traits::<impl core::fmt::Display for rug::integer::big::Integer>::fmt':
          fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer6traits75_$LT$impl$u20$core..fmt..Display$u20$for$u20$rug..integer..big..Integer$GT$3fmt17h0daeddf6bc85c589E+0x16): undefined reference to `__gmpz_sizeinbase'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer6traits75_$LT$impl$u20$core..fmt..Display$u20$for$u20$rug..integer..big..Integer$GT$3fmt17h0daeddf6bc85c589E+0x70): undefined reference to `__gmpz_get_str'
          /usr/bin/ld: fermat.4egfbnwy-cgu.0:(.text._ZN3rug7integer6traits75_$LT$impl$u20$core..fmt..Display$u20$for$u20$rug..integer..big..Integer$GT$3fmt17h0daeddf6bc85c589E+0x124): undefined reference to `__gmpz_get_str'
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

error: could not compile `fermat`

To learn more, run the command again with --verbose.

--
👇
12101111: export LDFLAGS=-lgmp

12101111 2020-11-30 10:04

export LDFLAGS=-lgmp

1 共 2 条评论, 1 页