Skip to content

Instantly share code, notes, and snippets.

@plusangel
Created October 31, 2020 19:04
Show Gist options
  • Save plusangel/108b4b8345ffc14a237b3daa468d390b to your computer and use it in GitHub Desktop.
Save plusangel/108b4b8345ffc14a237b3daa468d390b to your computer and use it in GitHub Desktop.
CMake dependent options
project(cmake_recipe01 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
# set(USE_LIBRARY OFF)
option(USE_LIBRARY "Compile sources into a library" OFF)
message(STATUS "Compile sources into a library? ${USE_LIBRARY}")
include(CMakeDependentOption)
cmake_dependent_option(
MAKE_STATIC_LIBRARY "Compile sources into a static library" OFF
"USE_LIBRARY" ON
)
cmake_dependent_option(
MAKE_SHARED_LIBRARY "Compile sources into a shared library" ON
"USE_LIBRARY" ON
)
# set(BUILD_SHARED_LIBS OFF)
list(APPEND _sources Message.h Message.cpp)
if(USE_LIBRARY)
message(STATUS "Compile sources into a STATIC library? ${MAKE_STATIC_LIBRARY}")
message(STATUS "Compile sources into a SHARED library? ${MAKE_SHARED_LIBRARY}")
if(MAKE_SHARED_LIBRARY)
add_library(message SHARED ${_sources})
add_executable(cmake_recipe01 main.cpp)
target_link_libraries(cmake_recipe01 message)
endif()
if(MAKE_STATIC_LIBRARY)
add_library(message STATIC ${_sources})
add_executable(cmake_recipe01 main.cpp)
target_link_libraries(cmake_recipe01 message)
endif()
else()
add_executable(cmake_recipe01 main.cpp ${_sources})
endif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment