cmake 教程

cmake 安装包

cmake ..\Step1 -G "MinGW Makefiles"

PROJECT_BINARY_DIR 在哪执行 cmake,这个值就是哪

PROJECT_SOURCE_DIR 项目根目录

教程3:库需要什么东西自己声明 INTERFACE_INCLUDE_DIRECTORIES : target_include_directories() INTERFACE 和 PUBLIC 关键字会把这个变量举起来,让库自己声明他需要 include 的文件。

CMAKE_CURRENT_SOURCE_DIR :当前源文件目录

list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions") 额外定义变量

target_include_directories 指定 include 目录

1. `PRIVATE`: The includes can only be used by the helpers-library itself.
2. `PUBLIC`: The includes can be used by the helpers-library itself and any target that uses the helpers-library, e.g. via `target_link_libraries(MainApplication PUBLIC libhelpers)`.
3. `INTERFACE`: The includes can't be used by the helpers-library, only by targets that use the helpers-library.

interface library:声明依赖,但不产生编译产物。好处:对特定目标使用不同的原则 https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries

target_compile_features 指定编译 feature

生成器表达式

CMAKE_GENERATOR: 环境变量,默认 generator。

0 1 表达式:A $<0:...> results in the empty string, and $<1:...> results in the content of .... They can also be nested.

BUILD_INTERFACE:仅编译时生效,install 不生效

compiler id:https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html

除了嵌入到代码中的测试之外,CTest 提供了可执行程序的测试方式。简而言之就是通过给可执行程序传入特定的参数,然后用 正则表达式 对它的输出进行匹配检查。通过这种方式可以很容易检查程序对于不正确的命令行参数的反应。这些测试定义在顶层的 CMakeLists.txt 文件中。

可以使用 ctest -R Usage -VV 命令来执行测试。这里给 ctest 的命令行参数:

  • -R <测试名称> : 执行单个测试
  • -VV:打印详细输出

1-6 done

cmake -D 指定参数

Configure using CMake Presets 跨平台共享配置 We recommend using CMake Presets for managing your CMake configurations. CMake Presets enable you to specify a common JSON file, where you store all the configurations for your project. You can then share this file with others, across different IDEs, and across different operating systems.

使用 CMakePresets.json 简化 CMake 项目初始化繁杂步骤 cmake –list-presents cmake –preset=ios-release-arm64 cmake –build –preset=ios-release-arm64

CMAKE_<LANG>_COMPILER:指定编译器路径

Questions about importing library file xxx.dll.a when generating dynamic library in window environment .dll.a(MinGW)/ .lib(MSVC) 是 windows linking 期间产生的临时文件。

扩展名: 源文件扩展名 常见的 C++ 源文件扩展名:

  • .cpp: 最为广泛使用的扩展名,代表 “C plus plus”。
  • .cxx: .cpp 的变体,主要用于避免命名冲突。
  • .cc: 与 .cpp 同义,也是常用的扩展名。
  • .C: 大写 C,有些编译器也支持,但容易与 C 语言源文件混淆,因此不推荐使用。
  • .c++: 与 .cpp 同义,但同样容易混淆,也不推荐使用。

其他较少见的扩展名:

  • .hpp, .hxx: 通常用于 C++ 头文件,但也可以用于源文件。
  • .CPP: 大写 CPP,有些编译器支持,但同样不推荐。

头文件扩展名 .h 是 C++ 头文件最常见的扩展名。那么,为什么有时候我们会看到 .hpp、.hxx 这样的扩展名呢?

.h、.hpp、.hxx 的区别

  • .h: 这是最传统、最广泛使用的 C++ 头文件扩展名。它简单明了,几乎所有的编译器都支持。
  • .hpp: 这个扩展名通常被认为是 “header plus plus” 的缩写,暗示着这是一个 C++ 风格的头文件。它主要用于区分 C 风格的头文件(通常用 .h)和 C++ 风格的头文件。
  • .hxx: 这个扩展名与 .hpp 类似,也是 .h 的变体,主要用于避免与某些系统或工具中的关键字或文件类型冲突。

三种设置 cmake compiler 的方法

cmake -B <path> 配置到指定目录

如果是在 CMakeLists.json 中用 set() 设置,要保证它在任何其他语句之前(如 project),否则就会使用默认的编译器(我被坑了)。

arm架构在 ubuntu跑不了


simon.li@intellyva:~/icomm/build$ ./ipc
-bash: ./ipc: cannot execute binary file: Exec format error
simon.li@intellyva:~/icomm/build$ file ipc
ipc: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-uClibc.so.0, not stripped


simon.li@intellyva:~/icomm/build$ uname -m
x86_64

CMakePresets.json 中 version 与 cmake 版本的 mapping 关系 写太高会无法识别。

cmake --list-presets

cmake --preset arm-debug