• 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
    
    • 1

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

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

    2.3 CMake-variables (predefined)

    VariableDescription
    ${CMAKE_SOURCE_DIR}CMake工作目录

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

    set(CMAKE_CXX_STANDARD 17)
    
    • 1

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

    命令格式:project( [...])

    Note
    项目名称不需要与项目根目录名称相同。

    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)
    
    • 1

    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}",
    
    • 1
    • 2

    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")
      
      • 1
    2. Set the CMAKE_BUILD_TYPE variable to Debug:
      set(CMAKE_BUILD_TYPE Debug)
      
      • 1
    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")
      
      • 1
    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)
    
    • 1
    • 2
  • 相关阅读:
    高效开发之:List去重简便方法
    如何计算 R 中的基尼系数(附示例)
    【K-means聚类算法】实现鸢尾花聚类
    Java注解
    Three.js添加父物体
    零代码编程:用ChatGPT多线程批量将PDF文档转换为word格式
    Conda常用命令和操作
    Vue3+typeScripte -03
    OpenCV-交互相关接口
    CTF 全讲解:[SWPUCTF 2021 新生赛]jicao
  • 原文地址:https://blog.csdn.net/songyuc/article/details/128018692