Dejwi
Dejwi

Reputation: 4487

CMake build subdirectory libraries as static ones

With the following project structure

CMakeLists.txt
libs\CMakeLists.txt

I've got a following use case: The CMakeLists.txt describes the build process of MyLib library. The libs\CMakeLists.txt describes the build process of libraries used by MyLib. It is used by main project with add_subdirectory().

I can control if MyLib will be a shared or static library with:

cmake -DBUILD_SHARED=TRUE|FALSE

All libs used by MyLib should be linked statically (MyLib should be a "standalone" library). But I do not want to make them static explicitly in libs\CMakeLists.txt

using add_library(... STATIC ...)

as they can be used in other project as shared ones.

Can I control how my add_subdirectory(project) will be build?

Upvotes: 3

Views: 1301

Answers (1)

Vertexwahn
Vertexwahn

Reputation: 8142

You can use an option:

option(MYLIB_BUILD_STATIC   "Build libraries as static libraries" ON)

# add/create library
if (MYLIB_BUILD_STATIC)
    add_definitions(-DMYLIB_STATIC_BUILD)
    add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES})
else (MYLIB_BUILD_STATIC)
    add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
endif (MYLIB_BUILD_STATIC)

Upvotes: 4

Related Questions