corrosion-rs / corrosion

Marrying Rust and CMake - Easy Rust and C/C++ Integration!
https://corrosion-rs.github.io/corrosion/
MIT License
1.02k stars 97 forks source link

[Bug]: corrosion_link_libraries does not include transitive dependencies #497

Closed swilcox3 closed 3 months ago

swilcox3 commented 3 months ago

Current Behavior

I'm wrapping a C++ library in a C interface and linking to it from Rust. Problem is, that C++ library links to hundreds of libraries transitively. CMake's target_link_libraries will figure out how to expand that list of dependencies and link everything correctly. corrosion_link_libraries doesn't add the transitive list of dependencies to the linker line. I've tried expanding out that list manually, but it's pretty hard to do between imported targets, built targets, libraries specified by full path, system libs, etc.

Expected Behavior

Is there a way to have corrosion link dependencies transitively, or to have rustc do it? If not, is there some way I could have CMake dump that linker line to a file so I could parse it and put it in the build.rs programmatically? I'm using gcc and ld with Ninja on the C++ side. Most of my dependencies are in a couple of directories, so I can specify the -L arguments just fine, I just need to enumerate everything that needs a -l parameter programmatically. I could enumerate it manually but it'd be fragile as heck.

Steps To Reproduce

Use corrosion_link_libraries on a library that has transitive dependencies.

Environment

- OS:Ubuntu 22.04
- CMake: 3.24.0
- CMake Generator: Ninja Multi-Config

CMake configure log with Debug log-level

No response

CMake Build step log

No response

swilcox3 commented 3 months ago

Some added context: this is what I saw when I tried the naive corrosion_link_libraries:

note: /home/swilcox/cf/master/conda_deps_lin64/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/../../../../x86_64-conda-linux-gnu/bin/ld: warning: libtrim.so, needed by /home/swilcox/cf/master/build_lin64/bin/Release/libflex_iga_interface.so, not found (try using -rpath or -rpath-link) libtrim.so is a first level dependency of the library I'm linking to, and it's in the same directory as the linked library. I've also added that directory directly with a cargo:rustc-link-search in my build.rs. I get this same warning for all of the library dependencies, which makes me think that this function doesn't work with transitive dependencies.

Should I just add all my dependency directories to LD_LIBRARY_PATH?

swilcox3 commented 3 months ago

Turns out I'm an idiot and LD_LIBRARY_PATH is the way to go to load all my libraries. I've still got a problem with an undefined symbol in libpthread, but I'm pretty sure that's because my entire C++ toolchain is in conda and we've got wires crossed somewhere.