0

I'm building an executable which should link dynamically with its dependency libraries, but - not all of them. In the case of one library, I want to link against its static version.

Now, the library I am after might be available both as a static and as a dynamic object. The find_package() command doesn't seem to let me say which of them I want ti find (or whether I want both to be found); and the documentation of target_link_libraries(), contains no text regarding how to choose between a static and dynamic variant of the same library.

Can I effect such a selection somehow? Or - is the only recourse a manual exposure of two targets, for the static and the dynamic targets pr

Note: In my specific case, the library is libtiff, but I doubt the answer is library-specific.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • "when I look for the library when building, it might be available both as a static and as a dynamic object." - If the library project provides two **targets** (shared and static), then just pass static target to `target_link_libraries`. Not sure what is a problem. – Tsyvarev Jun 08 '23 at 09:33
  • @Tsyvarev: And do libraries for which CMake distributes a find module typically provide two targets? And if not - why not? – einpoklum Jun 08 '23 at 11:20
  • "do libraries for which CMake distributes a find module typically provide two targets?" - No, as far as I know, most of Find modules provides only a single target, related to the shared library. Some Find modules provides a "switch" variable, which could be set for make the resulted target to represent a static library instead of a shared one. "And if not - why not?" - We could only speculate why developers of a specific Find module doesn't provide two targets. May be they don't want to write complex code for support static libraries, as their dependencies should be listed manually. – Tsyvarev Jun 08 '23 at 12:01
  • If your question post is about using `find_package` for obtain shared or static library, then please **clarify** that in the question post. You currently note documentation of `target_link_libraries` command, but what do you expect to find in it? That command accept a **target** or a **file**. A single target in CMake may represent only **one kind** of the library. So if you want to switch between static and shared libraries, then you need to look into specific Find (or Config) script and its documentation. – Tsyvarev Jun 08 '23 at 12:05
  • "I doubt the answer is library-specific" - The most generic solution I know is setting `CMAKE_FIND_LIBRARY_SUFFIXES` to the extension of a static library: https://stackoverflow.com/a/58348323/3440745. But it may do not work for some packages. All other solutions are library-specific. – Tsyvarev Jun 08 '23 at 15:11
  • related to the discussion in the comments, https://stackoverflow.com/q/2152077/11107541, and https://alexreinking.com/blog/building-a-dual-shared-and-static-library-with-cmake.html – starball Jun 09 '23 at 01:50
  • I've filed an issue against CMake about this: [issue 24988](https://gitlab.kitware.com/cmake/cmake/-/issues/24988). – einpoklum Jun 09 '23 at 14:21

0 Answers0