CMake之Presets编写

文章介绍了CMakePresets.json在CMake工程实践中的重要性,它允许开发者定义并管理不同编译器、平台和构建类型的预设,简化了在不同环境间的切换过程。VSCode插件的集成进一步增强了IDE的便捷性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在CMake工程实践中, 经常需要使用不同的配置编译来编译代码,比如要编译Debug、Relase版本,32位、64位版本,动态链接,静态链接, 使用不同的编译器, 如gcc、msvc, 不同的平台,如Windows、Linux、Android等。如果在编译时在命令行中输入参数,或者临时改CMake脚本代码就会很麻烦,这时CMakePresets.json配置文件就派上用场了。

CMakePresets.json 文件是 CMake 项目中的一个关键文件,它允许开发者定义一组预配置的构建设置,这些设置可以针对不同的编译器、平台和构建类型。通过这个文件,开发者可以轻松地在不同的构建环境之间切换,而无需手动修改 CMakeLists.txt 或其他构建脚本。

在 CMakePresets.json 文件中,可以定义多个预设(presets),每个预设包含一系列构建选项,如编译器路径、编译标志、目标架构等。例如,一个项目可能需要为 Debug 和 Release 构建类型分别定义不同的预设。

一个典型的 CMakePresets.json 文件可能包含以下内容:

  • name:预设的名称。
  • displayName:预设的显示名称。
  • description:预设的描述。
  • generator:指定生成器(例如 "Unix Makefiles"、"Ninja" 等)。
  • configuration:指定构建类型(例如 "Debug"、"Release" 等)。
  • cacheVariables:定义缓存变量。
  • env:定义环境变量。
  • sourceDir:项目源代码目录。
  • binaryDir:构建输出目录。
  • buildFlags:编译时使用的额外标志。
  • toolchainFile:工具链文件的路径。
  • inherits:继承(重用配置,相同的覆盖)的配置项名称

此外,CMakePresets.json 还支持配置 workflow 来决定项目的构建阶段,包括 configure、build、test 和 package 阶段。如果项目中不使用 CTest 和 CPack,那么 test 和 package 阶段可以省略不配置。

下面是我使用的一个CMakePresets.json配置文件,里面包含了ConfigPreset, BuildPreset, TestPreset,

{
    "version": 3,
    "cmakeMinimumRequired": {
        "major": 3,
        "minor": 10,
        "patch": 0
    },
    "configurePresets": [
        {
            "name": "linux-config-base",
            "hidden": true,
            "displayName": "base Configuration",
            "description": "",
            "generator": "Unix Makefiles",
            "binaryDir": "${sourceDir}/build/${presetName}",
            "toolchainFile": "/App/vcpkg/scripts/buildsystems/vcpkg.cmake",
            "condition":{
                "type": "equals",
                "lhs": "Linux",
                "rhs": "${hostSystemName}"
            },
            "cacheVariables": {
                "Z_VCPKG_ROOT_DIR": {
                    "value": "$env{ROOT_PATH}",
                    "type": "PATH"
                },
                "CMAKE_BUILD_TYPE": {
                    "value": "Debug",
                    "type": "string"
                },
                "VCPKG_TARGET_TRIPLET": {
                    "value": "x64-linux-static",
                    "type": "string"
                }
            },
            "environment": {
                "ROOT_PATH": "/App/vcpkg",
                "MY_ROOT":"/home/feixinz/1234"
            }
        },
        {
            "name": "linux-x64-static-debug",
            "displayName": "linux-x64-static-debug",
            "description": "Linux平台64位静态Debug版",
            "inherits":"linux-config-base",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": {
                    "value": "Debug",
                    "type": "string"
                },
                "VCPKG_TARGET_TRIPLET": {
                    "value": "x64-linux-static",
                    "type": "string"
                }
            }
        },
        {
            "name": "linux-x64-static-release",
            "displayName": "linux-x64-static-release",
            "inherits":"linux-config-base",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": {
                    "value": "Release",
                    "type": "string"
                },
                "VCPKG_TARGET_TRIPLET": {
                    "value": "x64-linux-static",
                    "type": "string"
                }
            }
        },
        {
            "name": "linux-x64-debug",
            "displayName": "linux-x64-debug",
            "inherits":"linux-config-base",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": {
                    "value": "Debug",
                    "type": "string"
                },
                "VCPKG_TARGET_TRIPLET": {
                    "value": "x64-linux",
                    "type": "string"
                }
            }
        },
        {
            "name": "linux-x64-release",
            "displayName": "linux-x64-release",
            "inherits":"linux-config-base",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": {
                    "value": "Release",
                    "type": "string"
                },
                "VCPKG_TARGET_TRIPLET": {
                    "value": "x64-linux",
                    "type": "string"
                }
            }
        }
    ],
    "buildPresets": [
        {
            "name":"linux-build-base",
            "hidden": true,
            "condition":{
                "type": "equals",
                "lhs": "Linux",
                "rhs": "${hostSystemName}"
            },
            "jobs": 8
        },
        {
            "name": "linux-x64-static-debug",
            "configurePreset": "linux-x64-static-debug",
            "inherits":"linux-build-base"
            
        },
        {
            "name": "linux-x64-static-release",
            "configurePreset": "linux-x64-static-release",
            "inherits":"linux-build-base"
        },
        {
            "name": "linux-x64-debug",
            "configurePreset": "linux-x64-debug",
            "inherits":"linux-build-base"
        },
        {
            "name": "linux-x64-release",
            "configurePreset": "linux-x64-release",
            "inherits":"linux-build-base"
        }
    ],
    "testPresets": [
        {
            "name": "llinux-x64-debug",
            "displayName": "linux-x64-debug",
            "configurePreset": "linux-x64-debug"
        }
    ]
}

如果vscode安装了CMake插件,再写一个CMakePresets.json文件,那么vscode就是一个简化版的IDE工具了,可以鼠标点击切换配置,选择编译的目标文件,调试,运行测试用例,打包,或者执行自定义脚本任务.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值