• VS中cmake多配置构建设置


    一.cmake在vs中的使用简介

    CMake 是一种跨平台开源工具,用于定义在多个平台上运行的生成过程。

    Visual Studio C 和 C++ 开发不再仅适用于 Windows。 本教程介绍如何在 Windows 和 Linux 上使用 Visual Studio C++ 进行跨平台开发。 由于它基于 CMake,无需创建或生成 Visual Studio 项目。 打开包含 CMakeLists.txt 文件的文件夹时,Visual Studio 会自动配置 IntelliSense 并生成设置。 可以快速开始在 Windows 上本地编辑、生成和调试代码。 然后,切换配置,在 Linux 上执行相同的操作,所有这些操作都从 Visual Studio 中进行。

    针对常见的开源项目,遵循以下步骤,可以快速在本地IDE VS中生成可以调试的项目代码

    • 从 GitHub 克隆一个开源 CMake 项目(或者下载到本地的项目源代码)
    • 在 Visual Studio 中打开项目(打开包含 CMakeLists.txt 文件的文件夹)
    • 在 Windows 上生成和调试可执行目标
    • 添加与 Linux 计算机的连接
    • 在 Linux 上生成和调试相同的目标

    二.一个在vs下cmake配置的项目包含的文件

    1.CMakeLists.txt

      包含在项目源码根目录的CMakeLists.txt与包含在单个项目文件夹中的CMakeLists.txt

    2.CMakeSettings.json

    vs对cmake的配置,可以方便的修改里面的属性字段达到预想的vs项目配置

    三.在CMakeSettings.json中配置实现cmake多配置构建设置

    单配置构建输出效果:

    只有一个debug,无法切换

    多配置构建输出效果:

    同时包含Debug,MinSizeRel,Release,RelWithDebinfo四种支持的构建方式

    "configurationType": "Debug;Release;RelWithDebInfo;MinSizeRel",

    在configurationType字段设置四种支持的构建方式

    如下编译kicad时的一个案例CMakeSettings.json

    1. {
    2. "environments": [
    3. {
    4. "BuildDir": "${workspaceRoot}\\build",
    5. "InstallDir": "${workspaceRoot}\\build\\install"
    6. },
    7. {
    8. "environment": "vcpkg",
    9. "VcPkgDir": "E:/vs2022/Projects/vcpkg"
    10. },
    11. {
    12. "environment": "mingw_64",
    13. "MINGW64_ROOT": "D:/msys64/mingw64",
    14. "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
    15. "FLAVOR": "x86_64-w64-mingw32",
    16. "TOOLSET_VERSION": "10.2.0",
    17. "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
    18. "INCLUDE": "${env.INCLUDE};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/tr1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/${env.FLAVOR}"
    19. }
    20. ],
    21. "configurations": [
    22. {
    23. "name": "x86-Debug",
    24. "generator": "Ninja",
    25. "configurationType": "Debug",
    26. "inheritEnvironments": [ "msvc_x86", "vcpkg" ],
    27. "buildRoot": "${env.BuildDir}\\${name}",
    28. "installRoot": "${env.InstallDir}\\${name}",
    29. "addressSanitizerEnabled": false,
    30. "cmakeCommandArgs": "",
    31. "buildCommandArgs": "-v",
    32. "ctestCommandArgs": "",
    33. "variables": [
    34. {
    35. "name": "KICAD_BUILD_QA_TESTS",
    36. "value": "False",
    37. "type": "BOOL"
    38. },
    39. {
    40. "name": "KICAD_WIN32_DPI_AWARE",
    41. "value": "ON",
    42. "type": "BOOL"
    43. }
    44. ],
    45. "cmakeToolchain": "${env.VcPkgDir}/scripts/buildsystems/vcpkg.cmake"
    46. },
    47. {
    48. "name": "x86-Release",
    49. "generator": "Ninja",
    50. "configurationType": "RelWithDebInfo",
    51. "inheritEnvironments": [ "msvc_x86", "vcpkg" ],
    52. "buildRoot": "${env.BuildDir}\\${name}",
    53. "installRoot": "${env.InstallDir}\\${name}",
    54. "cmakeCommandArgs": "",
    55. "buildCommandArgs": "-v",
    56. "ctestCommandArgs": "",
    57. "variables": [
    58. {
    59. "name": "KICAD_BUILD_QA_TESTS",
    60. "value": "False",
    61. "type": "BOOL"
    62. },
    63. {
    64. "name": "KICAD_WIN32_DPI_AWARE",
    65. "value": "ON",
    66. "type": "BOOL"
    67. }
    68. ],
    69. "cmakeToolchain": "${env.VcPkgDir}/scripts/buildsystems/vcpkg.cmake"
    70. },
    71. {
    72. "name": "x64-Debug",
    73. "generator": "Visual Studio 17 2022 Win64",
    74. "configurationType": "Debug;Release;RelWithDebInfo;MinSizeRel",
    75. "inheritEnvironments": [ "msvc_x64_x64", "vcpkg" ],
    76. "buildRoot": "${env.BuildDir}\\${name}",
    77. "installRoot": "${env.InstallDir}\\${name}",
    78. "cmakeCommandArgs": "-DSWIG_EXECUTABLE=E:/vs2022/Projects/vcpkg/swigwin-4.1.1/swig.exe",
    79. "buildCommandArgs": "-V",
    80. "ctestCommandArgs": "",
    81. "variables": [
    82. {
    83. "name": "KICAD_BUILD_QA_TESTS",
    84. "value": "False",
    85. "type": "BOOL"
    86. },
    87. {
    88. "name": "KICAD_WIN32_DPI_AWARE",
    89. "value": "ON",
    90. "type": "BOOL"
    91. }
    92. ],
    93. "cmakeToolchain": "${env.VcPkgDir}/scripts/buildsystems/vcpkg.cmake"
    94. },
    95. {
    96. "name": "x64-Release",
    97. "generator": "Visual Studio 17 2022 Win64",
    98. "configurationType": "RelWithDebInfo",
    99. "inheritEnvironments": [ "msvc_x64_x64", "vcpkg" ],
    100. "buildRoot": "${env.BuildDir}\\${name}",
    101. "installRoot": "${env.InstallDir}\\${name}",
    102. "cmakeCommandArgs": "-DSWIG_EXECUTABLE=E:/vs2022/Projects/vcpkg/swigwin-4.1.1/swig.exe",
    103. "buildCommandArgs": "-V",
    104. "ctestCommandArgs": "",
    105. "variables": [
    106. {
    107. "name": "KICAD_BUILD_QA_TESTS",
    108. "value": "False",
    109. "type": "BOOL"
    110. },
    111. {
    112. "name": "KICAD_WIN32_DPI_AWARE",
    113. "value": "ON",
    114. "type": "BOOL"
    115. }
    116. ],
    117. "cmakeToolchain": "${env.VcPkgDir}/scripts/buildsystems/vcpkg.cmake"
    118. },
    119. {
    120. "name": "MSYS2-x64-Debug",
    121. "generator": "Ninja",
    122. "configurationType": "Debug",
    123. "inheritEnvironments": [ "mingw_64" ],
    124. "buildRoot": "${env.BuildDir}\\${name}",
    125. "installRoot": "${env.InstallDir}\\${name}",
    126. "cmakeCommandArgs": "",
    127. "buildCommandArgs": "-v",
    128. "ctestCommandArgs": "",
    129. "intelliSenseMode": "linux-gcc-x64",
    130. "cmakeToolchain": "",
    131. "variables": [
    132. {
    133. "name": "CMAKE_C_COMPILER",
    134. "value": "${env.BIN_ROOT}/gcc.exe"
    135. },
    136. {
    137. "name": "CMAKE_CXX_COMPILER",
    138. "value": "${env.BIN_ROOT}/g++.exe"
    139. },
    140. {
    141. "name": "KICAD_BUILD_QA_TESTS",
    142. "value": "False",
    143. "type": "BOOL"
    144. },
    145. {
    146. "name": "KICAD_WIN32_DPI_AWARE",
    147. "value": "ON",
    148. "type": "BOOL"
    149. }
    150. ]
    151. }
    152. ]
    153. }

    参考资料:1.Visual Studio 中的 CMake 项目 | Microsoft Learn

  • 相关阅读:
    2021年SpringBoot面试题30道
    机组运行约束对机组节点边际电价的影响研究(Matlab代码实现)
    【漏洞复现】通达OA share身份认证绕过漏洞
    【Windows】windows7/10/11通用,开放防火墙入站、出站端口配置
    【23真题】发错试卷?想多了,只是题型大改!
    安卓JNI使用OpenCV
    软件测试的发展与定义
    vxe-table 解决show-overflow只显示弹窗不显示省略号或默认样式有省略号,弹窗不出现问题
    PC_非连续内存分配方式@分页存储管理@地址变换机构@快表
    OCPP1.6协议
  • 原文地址:https://blog.csdn.net/qq_22642239/article/details/133139434