'Tell cmake to build static version of the library

Let's consider CuteLogger CMakeLists.txt, it contains

ADD_LIBRARY(${library_target} SHARED ${sources} ${includes})

which instructs cmake to build the shared version (.dylib on macOS). I know I can change that to

ADD_LIBRARY(${library_target} STATIC ${sources} ${includes})

and it will produce, instead, the static version (.a on macOS).

Can I tell cmake to build the static version (from the command line) even when CMakeLists.txt ADD_LIBRARY is set to SHARED?

I tried

cmake .. -DBUILD_SHARED_LIBRARIES=False

but that does not seem to work.



Solution 1:[1]

You could try to add this if/else control flow to your CMakeLists.txt

if (BUILD_SHARED_LIBRARIES)
   ADD_LIBRARY(${library_target} SHARED ${sources} ${includes})
else()
   ADD_LIBRARY(${library_target} STATIC ${sources} ${includes})
endif()

And then pass the BUILD_SHARED_LIBRARIES to cmake from command line:

cmake .. -DBUILD_SHARED_LIBRARIES=False

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ramsay