一 内容
-
请先阅读 CMake Tutorial1_2 将CMake变量传递给代码源文件 和 CMake Tutorial2_1 创建并添加库到工程。
-
主要说明 如何通过变量选择使用的库。
-
文件目录
step2_2 |- MathFunctions |- CMakeLists.txt |- MathFunctions.h |- mysqrt.cc |- CMakeaLists.txt |- main.cc |- TutorialConfig.h.in
-
修改CMakeLists.txt
# 设置CMake最低版本
cmake_minimum_required(VERSION 3.16)
# 设置项目名称及版本
project(Tutorial VERSION 1.0)
# 生成可执行文件
add_executable(Tutorial main.cc)
# 设置选项
option(USE_MYMATH "use Turotial math" ON)
# 配置文件:拷贝文件到另一位置,并且修改其内容
configure_file(TutorialConfig.h.in TutorialConfig.h)
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND extra_libs MathFunctions)
list(APPEND extra_includes "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()
# 注意:extra_libs不存在时并未报错,extra_includes同样如此
target_link_libraries(Tutorial PUBLIC ${extra_libs})
# 打印信息
message("PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR}")
# 增加include路径到目标,否则会无法include生成的TutorialConfig.h
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" ${extra_includes})
- 修改TutorialConfig.h.in
// the configured options and setting for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#cmakedefine USE_MYMATH // add
- 修改 main.cc
#include <iostream>
#include "TutorialConfig.h" // cmake时会自动生成
#ifdef USE_MYMATH
#include "MathFunctions.h"
#endif
int main() {
#ifdef USE_MYMATH
int out = mysqrt(1);
std::cout << "USE_MYMATH defined" << std::endl;
#else
int out = sqrt(1);
std::cout << "USE_MYMATH undefined" << std::endl;
#endif
std::cout << "sqrt of 1 =" << out << std::endl;
return 0;
}
二 构建
- 与之前Tutorial相同,创建并进入构建目录
lee@leedeMacBook-Pro build % cmake ..
-- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
PROJECT_BINARY_DIR=/Users/lee/research/cmake_learn/cmake_tutorial/step2_2/build
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/lee/research/cmake_learn/cmake_tutorial/step2_2/build
lee@leedeMacBook-Pro build % ls
CMakeCache.txt Makefile TutorialConfig.h
CMakeFiles MathFunctions cmake_install.cmake
lee@leedeMacBook-Pro build % cat CMakeCache.txt
...
//use Turotial math
USE_MYMATH:BOOL=ON
...
- 查看CMakeCache.txt ,option的描述信息成为了变量的注释内容
- 查看TutorialConfig.h
lee@leedeMacBook-Pro build % cat TutorialConfig.h
// the configured options and setting for Tutorial
#define Tutorial_VERSION_MAJOR 1
#define Tutorial_VERSION_MINOR 0
#define USE_MYMATH
- 构建执行
lee@leedeMacBook-Pro build % cmake --build .
[ 25%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
[ 75%] Building CXX object CMakeFiles/Tutorial.dir/main.cc.o
[100%] Linking CXX executable Tutorial
[100%] Built target Tutorial
lee@leedeMacBook-Pro build % ./Tutorial
Computing sqrt of 1 to be 1
Computing sqrt of 1 to be 1
Computing sqrt of 1 to be 1
Computing sqrt of 1 to be 1
Computing sqrt of 1 to be 1
Computing sqrt of 1 to be 1
Computing sqrt of 1 to be 1
Computing sqrt of 1 to be 1
Computing sqrt of 1 to be 1
Computing sqrt of 1 to be 1
USE_MYMATH defined
sqrt of 1 =1
- 修改配置命令为 cmake … -DUSE_MYMATH=OFF
lee@leedeMacBook-Pro build % cmake .. -DUSE_MYMATH=OFF
PROJECT_BINARY_DIR=/Users/lee/research/cmake_learn/cmake_tutorial/step2_2/build
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/lee/research/cmake_learn/cmake_tutorial/step2_2/build
lee@leedeMacBook-Pro build % ls
CMakeCache.txt Makefile Tutorial cmake_install.cmake
CMakeFiles MathFunctions TutorialConfig.h
lee@leedeMacBook-Pro build % cat CMakeCache.txt
...
//use Turotial math
USE_MYMATH:BOOL=OFF
...
- 查看TutorialConfig.h
lee@leedeMacBook-Pro build % cat TutorialConfig.h
// the configured options and setting for Tutorial
#define Tutorial_VERSION_MAJOR 1
#define Tutorial_VERSION_MINOR 0
/* #undef USE_MYMATH */
- 注意与上面的区别。
- 构建执行
lee@leedeMacBook-Pro build % cmake --build .
PROJECT_BINARY_DIR=/Users/lee/research/cmake_learn/cmake_tutorial/step2_2/build
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/lee/research/cmake_learn/cmake_tutorial/step2_2/build
Consolidate compiler generated dependencies of target Tutorial
[ 50%] Building CXX object CMakeFiles/Tutorial.dir/main.cc.o
[100%] Linking CXX executable Tutorial
[100%] Built target Tutorial
lee@leedeMacBook-Pro build % ./Tutorial
USE_MYMATH undefined
sqrt of 1 =1
三 Github
- 代码已上传Github