How to specify connection type in CMake?

In my CMake script, I need to specify, for another library that my project is associated with, different types of links for gcc. It is well known to use the -Wl,-Bstatic and -Wl,-Bdynamic for such mixing. But can this be somehow specified in a cmake script?

+4
source share
2 answers

We use a couple of macros that adjust the preferred CMake search order on Linux / MacOSX to switch between dynamically and statically linked libraries.

 macro( prefer_static ) if( NOT WIN32 ) list( REMOVE_ITEM CMAKE_FIND_LIBRARY_SUFFIXES ".a" ) list( INSERT CMAKE_FIND_LIBRARY_SUFFIXES 0 ".a" ) endif() endmacro() macro( prefer_dynamic ) if( NOT WIN32 ) list( REMOVE_ITEM CMAKE_FIND_LIBRARY_SUFFIXES ".a" ) list( APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".a" ) endif() endmacro() 

we call the appropriate prefer_static() or prefer_dynamic() procedure before calling find_library(...) or find_package(...) . This has the advantage of β€œbacktracking” from the shared library when the static library is not available or vice versa.

This will not work for building Windows because you always reference the .lib file with Visual Studio and (AFAIK), there is no easy way to determine if it is a static or dynamic library.

+1
source

In CMake, find_library can be used for this purpose.

find_library(VAR libMyLib.a) OR SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a") find_library(VAR MyLib)

0
source

Source: https://habr.com/ru/post/1479177/


All Articles