CMake force linking static libs
CMake by default searches for shared libs before static libs.
Sometimes a project needs to specifically link external or internal static libs, even if shared libs are present.
A simple cross-platform way to do this is to set
CMAKE_FIND_LIBRARY_SUFFIXES
before any find_library()
or find_package()
:
if(NOT BUILD_SHARED_LIBS)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)
endif()
This takes advantage of de facto static library extensions for macOS, Windows and Linux, using CMake system variables.