• 使用CMake进行C++项目管理


    1、CMakeLists.txt基本结构

    使用VS2022创建一个CMake工程后,生成的默认配置文件CMakeLists.txt如下:

    1. # CMakeList.txt: CMakeTest 的 CMake 项目,在此处包括源代码并定义
    2. # 项目特定的逻辑。
    3. #
    4. cmake_minimum_required (VERSION 3.8)
    5. # 如果支持,请为 MSVC 编译器启用热重载。
    6. if (POLICY CMP0141)
    7. cmake_policy(SET CMP0141 NEW)
    8. set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$,$>,$<$:EditAndContinue>,$<$:ProgramDatabase>>")
    9. endif()
    10. project ("CMakeTest")
    11. # 将源代码添加到此项目的可执行文件。
    12. add_executable (CMakeTest "CMakeTest.cpp" "CMakeTest.h")
    13. if (CMAKE_VERSION VERSION_GREATER 3.12)
    14. set_property(TARGET CMakeTest PROPERTY CXX_STANDARD 20)
    15. endif()
    16. # TODO: 如有需要,请添加测试并安装目标。

    将CMakeSettings.json修改,连接到远程Linux服务器。

    1. {
    2. "configurations": [
    3. {
    4. "name": "Linux-GCC-Debug",
    5. "generator": "Unix Makefiles",
    6. "configurationType": "Debug",
    7. "cmakeExecutable": "cmake",
    8. "remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ],
    9. "cmakeCommandArgs": "",
    10. "buildCommandArgs": "",
    11. "ctestCommandArgs": "",
    12. "inheritEnvironments": [ "linux_x64" ],
    13. "remoteMachineName": "${defaultRemoteMachineName}",
    14. "remoteCMakeListsRoot": "$HOME/${projectDirName}/src",
    15. "remoteBuildRoot": "$HOME/${projectDirName}/build/${name}",
    16. "remoteInstallRoot": "$HOME/${projectDirName}/install/${name}",
    17. "remoteCopySources": true,
    18. "rsyncCommandArgs": "-t --delete",
    19. "remoteCopyBuildOutput": false,
    20. "remoteCopySourcesMethod": "rsync"
    21. },
    22. {
    23. "name": "Linux-GCC-Release",
    24. "generator": "Unix Makefiles",
    25. "configurationType": "Release",
    26. "cmakeExecutable": "cmake",
    27. "remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ],
    28. "cmakeCommandArgs": "",
    29. "buildCommandArgs": "",
    30. "ctestCommandArgs": "",
    31. "inheritEnvironments": [ "linux_x64" ],
    32. "remoteMachineName": "${defaultRemoteMachineName}",
    33. "remoteCMakeListsRoot": "$HOME/${projectDirName}/src",
    34. "remoteBuildRoot": "$HOME/${projectDirName}/build/${name}",
    35. "remoteInstallRoot": "$HOME/${projectDirName}/install/${name}",
    36. "remoteCopySources": true,
    37. "rsyncCommandArgs": "-t --delete",
    38. "remoteCopyBuildOutput": false,
    39. "remoteCopySourcesMethod": "rsync"
    40. }
    41. ]
    42. }

    2、添加宏定义

     (1)add_definitions定义宏

    对于无值宏:add_definitions(-DBOOST_ALL_DYN_LINK)
    对应于C语言中的  #define BOOST_ALL_DYN_LINK
    对有值宏:add_definitions(-DLIBEVENT_VERSION_NUMBER=0x02010800)
    对应C语言:#define LIBEVENT_VERSION_NUMBER 0x02010800

    (2)add_compile_definitions定义宏

    这个指令需要高cmake版本才能支持。比如:add_compile_definitions(MG_ENABLE_OPENSSL=1)
    对应于C语言中的  #define MG_ENABLE_OPENSSL 1
    add_compile_definitions(BOOST_LOG_DYN_LINK)

    CMakeLists.txt 文件中定义的宏在后面C/C++程序中是可以直接使用。

    1. add_definitions(-DBOOST_ALL_DYN_LINK)
    2. add_definitions(-DZLIB_VERSION 0X21)
    3. add_compile_definitions(ZLIB_VERSION=0X21)
    4. add_compile_definitions(BOOST_ALL_DYN_LINK)

    3、设置GCC路径

    set(CMAKE_C_COMPULER \"/usr/local/bin/gcc\")
    set(CMAKE_CXX_COMPULER \"/usr/local/bin/g++\")

    4、头文件和库

    1. # 1. 指定头文件位置
    2. target_include_directories(test PRIVATE "${PROJECT_SOURCE_DIR}/../loglib/include")
    3. # 2. 指定库文件搜索位置
    4. target_link_directories(test PRIVATE "${PROJECT_SOURCE_DIR}/../loglib/bin")
    5. # 3. 指定需要链接的库
    6. target_link_libraries(test -ltacopie -lcpo_redis)
    7. file(GLOB SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
    8. # 4. 生成可执行文件
    9. add_executable(test ${SRC_LIST})
    10. # 5. 生成动态链接库
    11. add_library(utility utility.cpp convert.cpp)
    12. add_executable(test.exe main.cpp)
    13. # 6. 链接库
    14. target_link_libraries(test.exe utility)
    15. #add_library写明了生成一个叫做utility.a的库文件,
    16. #然后和main.cpp编译出来的main.o生成可执行文件

    5、复制文件

    要在CMake中将配置文件复制到输出目录,两种方式
    (1)configure_file(setting.xml ${CMAKE_CURRENT_BINARY_DIR}/setting.xml COPYONLY)
    (2)file(COPY setting.xml DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

    6、QT库配置

  • 相关阅读:
    docker-compose的安装与卸载
    常用图像卷积核类型小结
    第5章 - 二阶多智能体系统的协同控制 --> 连续时间含时延系统一致性
    车规级电感厂家揭秘共模电感烧了的可能原因
    GoLang语言基本代码整理
    C++基础——初始化列表
    回溯算法集合(全排列,组合,子集)
    Actipro Editors for WPF高级数据输入组件
    C语言编程陷阱(四)
    Mybatis SQL构建器
  • 原文地址:https://blog.csdn.net/eamon100/article/details/133148102