cmake-rs icon indicating copy to clipboard operation
cmake-rs copied to clipboard

return static libraries required for linking

Open aboseley opened this issue 3 years ago • 1 comments
trafficstars

After building a static library it would be great if we could get the required list of libraries that need to be passed to the rust linker. If the cmake files use targets this info will be available.

aboseley avatar Mar 21 '22 07:03 aboseley

I had the same issue and what I ended up is:

  • Use https://github.com/Interstellar-Network/cmake/blob/main/export_libs.cmake to generate cmake_generated_libs which contains a list of all STATIC and SHARED libs used by the given TARGET
  • Parse this file in build.rs to set the correct cargo:rustc-link-search and cargo:rustc-link-lib cf https://github.com/Interstellar-Network/rust-cxx-cmake-bridge

It works, and is reasonably resilient (ie OK for both local and system libs, STATIC and SHARED) but it is a messy way to workaround the lack of communication between rust and CMake.

IMPORTANT: if you want to link with IMPORTED lib ie all those coming from find_package (eg Boost) you MUST set them GLOBAL eg set_target_properties(Boost::filesystem PROPERTIES IMPORTED_GLOBAL TRUE)

I would happily take a cleaner approach if there is one I have missed!

n-prat avatar Mar 22 '22 17:03 n-prat