1. 加入库
- top CMakeLists
configure_file(TutorialConfig.h.in TutorialConfig.h)
add_subdirectory(MathFunctions)
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC MathFunctions)
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}")
- MathFunctions CMakeLists
add_library(MathFunctions MathFunctions.cxx mysqrt.cxx)
2. 使库成为一个选项
- MathFunctions CMakeLists
add_library(MathFunctions MathFunctions.cxx )
option(USE_MYMATH "use own math library" ON)
if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE -DUSE_MYMATH)
add_library(SqrtLibrary STATIC mysqrt.cxx)
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()
命令行
cmake ../ -DUSE_MYMATH=OFF
cmake --build .