CMake Tutorial2_2 设置使用库的开关

一 内容
  1. 请先阅读 CMake Tutorial1_2 将CMake变量传递给代码源文件CMake Tutorial2_1 创建并添加库到工程

  2. 主要说明 如何通过变量选择使用的库

  3. 文件目录

    step2_2
    |- MathFunctions
      |- CMakeLists.txt
      |- MathFunctions.h
      |- mysqrt.cc
    |- CMakeaLists.txt
    |- main.cc 
    |- TutorialConfig.h.in
    
  4. 修改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})
  1. 修改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
  1. 修改 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;
}
二 构建
  1. 与之前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的描述信息成为了变量的注释内容
  1. 查看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
  1. 构建执行
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
  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
...
  1. 查看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 */
  • 注意与上面的区别。
  1. 构建执行
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
四 参考
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值