$ git clone --depth=1 https://github.com/ProgCZ/code-cloud-a.git $ cd code-cloud-a/2020/03/cmake-tutorial
1 创建基础模板
根目录中的 CMakeLists.txt 文件一览:
cmake_minimum_required(VERSION 3.5)
# set the project name and version project(Tutorial VERSION 1.0)
# specify the C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON)
# configure a header file to pass some of the CMake settings # to the source code configure_file(TutorialConfig.h.in TutorialConfig.h)
# add the executable add_executable(Tutorial main.cpp)
# add the binary tree to the search path for include files # so that we will find TutorialConfig.h target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
1.1 设置 CMake 最低版本
# Format cmake_minimum_required(VERSION <min>[...<max>] [FATAL_ERROR]) # Example cmake_minimum_required(VERSION 3.5)
# Format target_include_directories(<target> [SYSTEM] [BEFORE] <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...]) # Example target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
$ mkdir build && cd build $ cmake .. && cmake --build . -- The C compiler identification is GNU 5.4.0 -- The CXX compiler identification is GNU 5.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc - works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ - works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/peter/Projects/test_cmake_ws/build Scanning dependencies of target Tutorial [ 50%] Building CXX object CMakeFiles/Tutorial.dir/main.cpp.o [100%] Linking CXX executable Tutorial [100%] Built target Tutorial
1.8 运行项目
$ ./Tutorial 10 The square root of 10 is 3.16228 $ ./Tutorial ./Tutorial Version: 1.0 Usage: ./Tutorial number
# state that anybody linking to us needs to include the current source dir # to find MathFunctions.h, while we don't. target_include_directories(MathFunctions INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
2.1 为库的用户添加库
# Format add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [source1] [source2 ...]) # Example add_library(MathFunctions mysqrt.cpp)
# add the executable add_executable(Tutorial main.cpp)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
# add the binary tree to the search path for include files # so that we will find TutorialConfig.h target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
2.3 传递 USE_MYMATH 选项
# Format option(<variable> "<help_text>" [value]) # Example option(USE_MYMATH "Use tutorial provided math implementation"ON)
修改头文件模板 TutorialConfig.h.in:
// the configured options and settings for Tutorial #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ #cmakedefine USE_MYMATH
# Format target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> <item>... [<PRIVATE|PUBLIC|INTERFACE> <item>...]...) # Example target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
$ cd build $ ctest -VV UpdateCTestConfiguration from :/home/peter/Projects/test_cmake_ws/build/DartConfiguration.tcl UpdateCTestConfiguration from :/home/peter/Projects/test_cmake_ws/build/DartConfiguration.tcl Test project /home/peter/Projects/test_cmake_ws/build Constructing a list of tests Done constructing a list of tests Updating test list for fixtures Added 0 tests to meet fixture requirements Checking test dependency graph... Checking test dependency graph end test 1 Start 1: Runs
1: Test command: /home/peter/Projects/test_cmake_ws/build/Tutorial "25" 1: Test timeout computed to be: 10000000 1: The square root of 25 is 5 1/4 Test #1: Runs ............................. Passed 0.03 sec test 2 Start 2: Usage
2: Test command: /home/peter/Projects/test_cmake_ws/build/Tutorial 2: Test timeout computed to be: 10000000 2: /home/peter/Projects/test_cmake_ws/build/Tutorial Version: 1.0 2: Usage: /home/peter/Projects/test_cmake_ws/build/Tutorial number 2/4 Test #2: Usage ............................ Passed 0.00 sec test 3 Start 3: Comp5
3: Test command: /home/peter/Projects/test_cmake_ws/build/Tutorial "5" 3: Test timeout computed to be: 10000000 3: The square root of 5 is 2.23607 3/4 Test #3: Comp5 ............................ Passed 0.00 sec test 4 Start 4: Comp-25
4: Test command: /home/peter/Projects/test_cmake_ws/build/Tutorial "-25" 4: Test timeout computed to be: 10000000 4: The square root of -25 is -nan 4/4 Test #4: Comp-25 .......................... Passed 0.01 sec
100% tests passed, 0 tests failed out of 4
Total Test time (real) = 0.12 sec
4 写在最后
未来一段时间可能会找一些优秀的 C++ 项目源码来阅读,学习编码技巧的同时也能进一步了解如何编写项目的 CMakeLists.txt。