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
指令的书写是大小写无关的;
| Variable | Description |
|---|---|
${CMAKE_SOURCE_DIR} | CMake工作目录 |
CMAKE_CXX_STANDARDset(CMAKE_CXX_STANDARD 17)
命令格式:project(
Note
项目名称不需要与项目根目录名称相同。
project(custom_dummy_plugin):指定了工程名字;
project(custom_dummy_plugin CUDA CXX C):指定了工程名字,并且支持语言是C和C++;
set(CMAKE_CXX_STANDARD 17):定义变量 CMAKE_CXX_STANDARD的值为17
set(SRC_LIST main.cpp t1.cpp t2.cpp)
link_directories("${DALI_LIB_DIR}"):链接DALI_LIB_DIR源代码目录。
官方文档:message — CMake Documentation
可以使用多种输出模式,常见的模式有:
—的信息。CMake Tools插件教程:《VSCode-cmake-tools/debug-launch.md | Debug using a launch.json file》
// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",
关于使用CMake命令寻找python解释器,请参考博文《C++ clion使用python》
官方文档:include_directories()
include_directories(SYSTEM "${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}"):添加CUDA目录
link_directories("${DALI_LIB_DIR}"):引入DALI库目录文件夹;
在使用link_directories引入工具库目录后,就可以用target_link_libraries引入链接库了,链接库可以用库名引入,库名的命名规则是:链接库文件名lib和.so中间的字段,例如:cublas库名则引用的是 libcublas.so。
请参考教程【从零开始详细介绍CMake:02:15 使用add_subdirectory关联子目录的CMakeList.txt】
cmake ..:在build目录中编译工程使用cmake ..时,一般是当前光标位于build文件夹中,而此时CMakeList.txt位于上一级主目录中,所以加上..来声明CMakeList.txt所在的目录位置。
在编译共享链接库时附加调试标记:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g")
有一次在编译时,出现这样的错误:
[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: -vThe 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: -vThe output was:
No such file or directory
…
从网上的资料来看,似乎是CMake未能找到CUDA编译器,我们尝试过手动指定CUDA的编译器,有时会修复这个问题:
#project(... LANGUAGES CUDA CXX C)
find_package(CUDAToolkit)