Linking issues! Problem in link with an object file from `c++`

Now, I have an object file – a black box to decryption RSA ciphertexts which is compiled with g++. when I try to link with rustc, I got the error messages:

  = note: /usr/bin/ld: /home/osmanthus/Projects/experiment/target/debug/build/experiment-4970444be695a1e7/out/libwrapper.a(dec.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
          /usr/bin/ld: final link failed: nonrepresentable section on output                        
          collect2: error: ld returned 1 exit status

With g++, I can fix it with linking options -no-pie, so I try to pass -no-pie to cc::Build:

extern crate cc;

fn main() {
    cc::Build::new()
        .file("src/wrapper.cpp")
        .cpp(true)
        .flag("-no-pie")
        .flag("-lgmp")
        .object("src/dec.o")
        .compile("libwrapper.a");
}

But I fail… How can I fix it within build.rs

Rust requires objects (static libraries) to be compiled with -fPIC. If the .o file wasn’t compiled with -fPIC, it won’t work.

2 Likes