rust-lang / flate2-rs

DEFLATE, gzip, and zlib bindings for Rust
https://docs.rs/flate2
Apache License 2.0
891 stars 158 forks source link

Linker error when trying to compile to a C++ callable library on Linux #281

Closed OscarTHZhang closed 1 year ago

OscarTHZhang commented 2 years ago

I was trying to use compile my code that uses this library into C++ callable static library using cxx.rs and corrosion following this example.

As my code is building, the following linker error appeared:

[ 57%] Generating ../cargo/build/x86_64-unknown-linux-gnu/cxxbridge/rust/cxx.h, ../cargo/build/x86_64-unknown-linux-gnu/cxxbridge/rusty_code/src/lib.rs.h, ../cargo/build/x86_64-unknown-linux-gnu/cxxbridge/rusty_code/src/lib.rs.cc
[ 57%] Generating ../cargo/build/x86_64-unknown-linux-gnu/cxxbridge/rust/cxx.h, ../cargo/build/x86_64-unknown-linux-gnu/cxxbridge/rusty_code/src/lib.rs.h, ../cargo/build/x86_64-unknown-linux-gnu/cxxbridge/rusty_code/src/lib.rs.cc
[ 71%] Linking CXX executable rusty_cmake
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.0.rcgu.o): in function `flate2::mem::Compress::set_dictionary':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/mem.rs:287: undefined reference to `deflateSetDictionary'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.0.rcgu.o): in function `flate2::mem::Compress::set_level':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/mem.rs:320: undefined reference to `deflateParams'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.0.rcgu.o): in function `flate2::mem::Decompress::set_dictionary':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/mem.rs:522: undefined reference to `inflateSetDictionary'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.15.rcgu.o): in function `flate2::ffi::c::c_backend::mz_deflateInit2':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/ffi/c.rs:425: undefined reference to `deflateInit2_'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.15.rcgu.o): in function `flate2::ffi::c::c_backend::mz_inflateInit2':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/ffi/c.rs:437: undefined reference to `inflateInit2_'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.2.rcgu.o): in function `<flate2::ffi::c::DirCompress as flate2::ffi::c::Direction>::destroy':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/ffi/c.rs:187: undefined reference to `deflateEnd'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.2.rcgu.o): in function `<flate2::ffi::c::DirDecompress as flate2::ffi::c::Direction>::destroy':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/ffi/c.rs:192: undefined reference to `inflateEnd'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.2.rcgu.o): in function `<flate2::ffi::c::Inflate as flate2::ffi::InflateBackend>::decompress':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/ffi/c.rs:238: undefined reference to `inflate'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.2.rcgu.o): in function `<flate2::ffi::c::Inflate as flate2::ffi::InflateBackend>::reset':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/ffi/c.rs:263: undefined reference to `inflateReset2'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.2.rcgu.o): in function `<flate2::ffi::c::Deflate as flate2::ffi::DeflateBackend>::compress':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/ffi/c.rs:332: undefined reference to `deflate'
/usr/bin/ld: librusty_code.a(flate2-72e888fd26eb5669.flate2.b75aca4e-cgu.2.rcgu.o): in function `<flate2::ffi::c::Deflate as flate2::ffi::DeflateBackend>::reset':
/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/ffi/c.rs:351: undefined reference to `deflateReset'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [src/CMakeFiles/rusty_cmake.dir/build.make:110: src/rusty_cmake] Error 1
make[2]: *** [CMakeFiles/Makefile2:230: src/CMakeFiles/rusty_cmake.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:237: src/CMakeFiles/rusty_cmake.dir/rule] Error 2
make: *** [Makefile:170: rusty_cmake] Error 2

By tracing the error message I noticed that the error is coming from flate2-rs. So I wonder if someone would know the solution for this cross-compiling linker error?

Thanks in advance!

oyvindln commented 2 years ago

It looks like it's failing to link to the C library functions called from flate2-rs when using zlib or miniz (assuming zlib since it's attempting to link *setDictionary). Does it work if using the rust miniz_oxide back-end (which is the default unless explicitly set to something else)? Maybe there is some issue with the C libraries not being built or located when using corrosion/cxx.rs?

Not sure how to solve this if you can't use the rust back-end, though I suspect it's more an issue with corrosion not fully handling rust code that in turn builds/links back to C code rather than an issue in flate2 itself.

esteve commented 2 years ago

@OscarTHZhang I ran into the same problem. One solution I found is to link the imported corrosion target against libz in your CMakeLists.txt. For example:

corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml)
target_link_libraries(MY_CORROSION_CRATE INTERFACE z)

where MY_CORROSION_CRATE is the name of your crate in the rust/Cargo.toml file.

esteve commented 2 years ago

@alexcrichton @joshtriplett this ticket should be ok to close with the solution I provided in https://github.com/rust-lang/flate2-rs/issues/281#issuecomment-1207277303