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.
source share