CMake入门-04-自定义编译选项

2019-08-23  本文已影响0人  binglingziyu

工作环境

Hello,World! - 自定义编译选项

CMake 允许为项目增加编译选项,从而可以根据用户的环境和需求选择最合适的编译方案。

例如,可以将 MathFunctions 库设为一个可选的库,如果该选项为 ON ,就使用该库定义的数学函数来进行运算。否则就调用标准库中的数学函数库。

(0) 初始化项目

$ mkdir hello
$ cd hello
$ mkdir math build
$ touch CMakeLists.txt main.cpp math/MathFunctions.h math/MathFunctions.cpp math/CMakeLists.txt
$ tree
.
├── build
├── CMakeLists.txt
├── main.cpp
└── math
    ├── CMakeLists.txt
    ├── MathFunctions.cpp
    └── MathFunctions.h
int power(int base, int exponent);
#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h"

int power(int base, int exponent) {
    int result = base;
    int i;

    if (exponent == 0) {
        return 1;
    }

    for(i = 1; i < exponent; ++i){
        result = result * base;
    }
    return result;
}
#include <iostream>
#include "MathFunctions.h"

using namespace std;

int main(int argc, char const *argv[]) {
  printf("%s power(2,3)=%d \n", "Hello,World!", power(2, 3));
  return 0;
}
# CMake 最低版本号要求
cmake_minimum_required(VERSION 3.15.0)

# 项目名
project(hello)

# 查找当前目录下的所有源文件,并将名称保存到 SRC_LIST 变量
aux_source_directory(. SRC_LIST)
# 查找 math 目录下的所有源文件,并将名称保存到 MATH_SRC_LIST 变量
# aux_source_directory(${PROJECT_SOURCE_DIR}/math MATH_SRC_LIST)

# 添加 math 子目录 (math 目录里必须有 CMakeLists.txt),这样 math 目录下的 CMakeLists.txt 文件和源代码也会被处理
add_subdirectory(math)

# 添加头文件路径
include_directories(${PROJECT_SOURCE_DIR}/math)

# 指定生成目标
add_executable(hello ${SRC_LIST} ${MATH_SRC_LIST})

# 添加链接库
target_link_libraries(hello MathFunctions)
# 查找当前目录下的所有源文件,并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)

# 生成链接库
add_library (MathFunctions ${DIR_LIB_SRCS})

(1) 修改根目录 CMakeLists.txt

cmake_minimum_required(VERSION 3.15.0)

# 项目名
project(hello)

# 查找当前目录下的所有源文件,并将名称保存到 SRC_LIST 变量
aux_source_directory(. SRC_LIST)


# 加入一个配置头文件,用于处理 CMake 对源码的设置
configure_file (
  "${PROJECT_SOURCE_DIR}/config.h.in"
  "${PROJECT_SOURCE_DIR}/config.h"
)

# 是否使用自己的 MathFunctions 库
# 这里设置的变量 USE_MYMATH、中间的提示文字、默认值,在 ccmake 命令中会展示
option (USE_MYMATH
  "Use provided math implementation"
  ON
)

# 是否加入 MathFunctions 库
if (USE_MYMATH)
  # 添加头文件路径
  include_directories ("${PROJECT_SOURCE_DIR}/math")
  # 添加 math 子目录 (math 目录里必须有 CMakeLists.txt)
  add_subdirectory (math)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)



# 指定生成目标
add_executable(hello ${SRC_LIST} ${MATH_SRC_LIST})

# 添加链接库
target_link_libraries(hello ${EXTRA_LIBS})

(2) 修改 main.cpp 文件

#include <iostream>
#include "config.h"

#ifdef USE_MYMATH
  // 如果定义了 USE_MYMATH,导入 "MathFunctions.h"
  #include "MathFunctions.h"
#else
  // 如果 USE_MYMATH 未定义,导入 <cmath>
  #include <cmath>
#endif

using namespace std;

int main(int argc, char const *argv[]) {

  #ifdef USE_MYMATH
    printf("Here define USE_MYMATH \n");
    printf("%s power(2,3)=%d \n", "Hello,World!", power(2, 3));
  #else
    printf("Here undefine USE_MYMATH \n");
    printf("%s power(2,3)=%f \n", "Hello,World!", pow(2, 3));
  #endif

  return 0;
}

(3) 新建 config.h.in 文件

#cmakedefine USE_MYMATH

(4) 编译 & 运行

$ cd Desktop/hello/build

# cmake 指定 USE_MYMATH=ON
$ cmake -DUSE_MYMATH=ON ..
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Users/staff/Desktop/hello/build

$  make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunctions.cpp.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target hello
[ 75%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello

# 这里输出的 ”Here define USE_MYMATH“
$ ./hello
Here define USE_MYMATH
Hello,World! power(2,3)=8

# cmake 指定 USE_MYMATH=OFF
$ cmake -DUSE_MYMATH=OFF ..
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/staff/Desktop/hello/build

$ make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello

# 这里输出的 ”Here undefine USE_MYMATH“
$ ./hello
Here undefine USE_MYMATH
Hello,World! power(2,3)=8.000000
$ cd Desktop/hello/build
$ ccmake ..

可以看到 USE_MYMATH 选项

ccmake-command

相关参考:
CMake 官方网站
CMake 入门实战


上一篇下一篇

猜你喜欢

热点阅读