CMake Cookbook by Eric

I. 学习教程

  1. 《【C++】《从零开始详细介绍CMake》- 知识点目录》

II. Basics

2.1 查看CMake版本

Note
Ubuntu:
Command ‘cmake’ not found, but can be installed with:
sudo snap install cmake # version 3.25.2, or
sudo apt install cmake # version 3.22.1-1ubuntu1.22.04.1
See ‘snap info cmake’ for additional versions.

cmake --version

2.2 关键字:CMake中的构建指令

指令的书写是大小写无关的;

2.3 CMake-variables (predefined)

VariableDescription
${CMAKE_SOURCE_DIR}CMake工作目录

1.4 指定使用的C++标准:CMAKE_CXX_STANDARD

set(CMAKE_CXX_STANDARD 17)

II. Project:指定项目名称和语言类型

命令格式:project(<PROJECT-NAME> [<language-name>...])

Note
项目名称<PROJECT-NAME>不需要与项目根目录名称相同。

Project示例

project(custom_dummy_plugin):指定了工程名字;
project(custom_dummy_plugin CUDA CXX C):指定了工程名字,并且支持语言是C和C++;

III. Set:定义变量(值和列表)

set(CMAKE_CXX_STANDARD 17):定义变量 CMAKE_CXX_STANDARD的值为17

Set定义列表示例

set(SRC_LIST main.cpp t1.cpp t2.cpp)

VI. ${V_NAME}:引用变量值

link_directories("${DALI_LIB_DIR}"):链接DALI_LIB_DIR源代码目录。

V. Message:向终端输出用户自定义的信息

官方文档:message — CMake Documentation
可以使用多种输出模式,常见的模式有:

  1. SEND_ERROR:产⽣错误,⽣成过程被跳过。
  2. SATUS:输出前缀为的信息。
  3. FATAL_ERROR:⽴即终⽌所有CMake过程。

5.1 在launch.json中配置cmake

CMake Tools插件教程:《VSCode-cmake-tools/debug-launch.md | Debug using a launch.json file》

// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",

5.2 在CMake中寻找python解释器

关于使用CMake命令寻找python解释器,请参考博文《C++ clion使用python》

VI. Include_directories:添加源代码目录

官方文档:include_directories()
include_directories(SYSTEM "${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}"):添加CUDA目录

VII. Execute_process:执行bash指令获得字符串参数

文档:execute_process()

VIII. Link_directories:引入工具库目录

link_directories("${DALI_LIB_DIR}"):引入DALI库目录文件夹;

IX. Target_link_libraries:引入链接库(target)

在使用link_directories引入工具库目录后,就可以用target_link_libraries引入链接库了,链接库可以用库名引入,库名的命名规则是:链接库文件名lib.so中间的字段,例如:cublas库名则引用的是 libcublas.so

X. Add_subdirectory:关联子目录中的CMakeList.txt

请参考教程【从零开始详细介绍CMake:02:15 使用add_subdirectory关联子目录的CMakeList.txt】

VI. 常见命令

cmake ..:在build目录中编译工程

使用cmake ..时,一般是当前光标位于build文件夹中,而此时CMakeList.txt位于上一级主目录中,所以加上..来声明CMakeList.txt所在的目录位置。

VII. 设置调试标记

在编译共享链接库时附加调试标记:

  1. Add the -g flag to the CMAKE_CXX_FLAGS variable in your CMakeLists.txt file:
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
    
  2. Set the CMAKE_BUILD_TYPE variable to Debug:
    set(CMAKE_BUILD_TYPE Debug)
    
  3. Enable the -g flag for the linker by adding the following line to your CMakeLists.txt file:
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g")
    
  4. Rebuild your project using the cmake and make commands.

Troubleshooting

(1)出现:The CUDA compiler identification is unknown…

有一次在编译时,出现这样的错误:

[Bash]: The CUDA compiler identification is unknown…
在这里插入图片描述
[CMakeError.log]:
Checking whether the CUDA compiler is NVIDIA using “” did not match “nvcc: NVIDIA (R) Cuda compiler driver”:
Checking whether the CUDA compiler is Clang using “” did not match “(clang version)”:
Compiling the CUDA compiler identification source file “CMakeCUDACompilerId.cu” failed.
Compiler: CMAKE_CUDA_COMPILER-NOTFOUND
Build flags:
Id flags: -v
 
The output was:
No such file or directory
 
 
Compiling the CUDA compiler identification source file “CMakeCUDACompilerId.cu” failed.
Compiler: CMAKE_CUDA_COMPILER-NOTFOUND
Build flags:
Id flags: -v

The output was:
 
No such file or directory
 
 
Checking whether the CUDA compiler is NVIDIA using “” did not match “nvcc: NVIDIA (R) Cuda compiler driver”:
Checking whether the CUDA compiler is Clang using “” did not match “(clang version)”:
Compiling the CUDA compiler identification source file “CMakeCUDACompilerId.cu” failed.
Compiler: CMAKE_CUDA_COMPILER-NOTFOUND
Build flags:
Id flags: -v

The output was:
No such file or directory

从网上的资料来看,似乎是CMake未能找到CUDA编译器,我们尝试过手动指定CUDA的编译器,有时会修复这个问题:

#project(... LANGUAGES CUDA CXX C)
find_package(CUDAToolkit)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值