2020-08-28
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Tutorial)
add_executable(Tutorial tutorial.cxx)
CMakeLists.txt
than in source codeproject(Tutorial VERSION 1.0)
configure_file(TutorialConfig.h.in TutorialConfig.h)
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
TutorialConfig.h.in
with
@Tutorial_VERSION_MAJOR@
@Tutorial_VERSION_MINOR@
PATCH
, TWEAK
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
💬 CXX_STANDARD_REQUIRED
cmake ..
cmake --build .
CMakeLists.txt
under MathFunctions folder
add_library(MathFunctions mysqrt.cxx)
CMakeLists.txt
add_subdirectory(MathFunctions)
target_link_libraries(Tutorial PUBLIC MathFunctions)
target_include_directories(… "${PROJECT_SOURCE_DIR}/MathFunctions")
option(USE_MYMATH "…" ON)
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
list(APPEND EXTRA_INCS "${PROJECT_SOURCE_DIR}/MathFunctions")
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
target_include_directories(… "${EXTRA_INCS}")
EXTRA_LIBS
/EXTRA_INCS
are classic approach → modern approach next step#ifdef USE_MYMATH
where in need#cmakedefine USE_MYMATH
in TutorialConfig.h.in
INTERFACE
– tell consumers what should be configed to use/link the targettarget_include_directories(MathFunctions INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
INTERFACE
: for consumerPUBLIC
: INTERFACE
+ PRIVATE
PRIVATE
: for producerMathFunctions/CMakeLists.txt
)
install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)
CMakeLists.txt
)
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" DESTINATION include)
cmake --install
(≧ v3.15)
cmake install
CMAKE_INSTALL_PREFIX
: where to install files
--prefix
to customizeenable_testing()
ctest -N
(dry run), ctest -VV
(extra verbose) under binary tree 💬 options
ctest -C Debug -VV
log
/exp
are not common (require m library instead)include(CheckSymbolExists)
check_symbol_exists(log "math.h" HAVE_LOG)
check_symbol_exists(exp "math.h" HAVE_EXP)
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP
HAVE_LOG
/HAVE_EXP
where in needTutorialConfig.h
target_include_directories(MathFunctions … PRIVATE ${CMAKE_BINARY_DIR})
HAVE_LOG
/HAVE_EXP
without TutorialConfig.h
TutorialConfig.h
MathFunctions/CMakeLists.txt
if(HAVE_LOG AND HAVE_EXP)
, specify private definitionstarget_compile_definitions(MathFunctions PRIVATE "HAVE_LOG" "HAVE_EXP")
log
/exp
anymore, and remove HAVE_LOG
/HAVE_EXP
#include <cmath>
add_executable(MakeTable MakeTable.cxx)
add_custom_command(OUTPUT …Table.h COMMAND MakeTable …Table.h DEPENDS MakeTable)
mysqrt.cxx
depends on Table.h
add_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h)
Table.h
can be includedtarget_include_directories(MathFunctions … PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
cmake
build this projectinclude(InstallRequiredSystemLibraries)
to include required runtime librariesinclude(CPack)
cpack
-G
: generator-c
: configurationcpack --config CPackSourceConfig.cmake
make package
include(CTest)
instead of enable_testing()
CTestConfig.cmake
at top-level directory, and specify variablesctest -VV -D Experimental
under binary treeBUILD_SHARED_LIBS
make add_library()
become SHARED
(if no type specified)USE_MYMATH
option to MathFunctions
MathFunctions
(static) links to SqrtLibrary
if USE_MYMATH
#include <cmath>
required)option(BUILD_SHARED_LIBS "…" ON)
DECLSPEC
with target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH")
POSITION_INDEPENDENT_CODE
💬 wiki: Position Independent Codeset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
$<0:…>
→ empty string$<1:…>
→ content of "…"INTERFACE
target to add compiler flags
$<COMPILE_LANG_AND_ID:language,compiler_ids>
– different flags for gcc/msvc$<BUILD_INTERFACE:…>
– not to apply on the consumers after installedinstall(TARGETS … EXPORT MathFunctionsTargets)
install(EXPORT … FILE MathFunctionsTargets.cmake DESTINATION …)
target_include_directories(MathFunctions INTERFACE … $<install_interface:include>)
find_package()
by CMakePackageConfigHelpers
Config.cmake.in
include(CMakePackageConfigHelpers)
configure_package_config_file(…)
for MathFunctionsConfig.cmake
write_basic_package_version_file(…)
for MathFunctionsConfigVersion.cmake
export(EXPORT MathFunctionsTargets FILE "${CMAKE_CURRENT_BINSRY_DIR}/MathFunctionsTargets.cmake")
set(CMAKE_DEBUG_POSTFIX d)
set_target_properties(Tutorial PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
set_property(TARGET MathFunctions PROPERTY VERSION "1.0.0")
set_property(TARGET MathFunctions PROPERTY SOVERSION "1")
MultiCPackConfig.cmake
to pack 2 builds into one
CPACK_INSTALL_CMAKE_PROJECTS
variablecpack --config MultiCPackConfig.cmake