gpt4 book ai didi

c++ - 在 CMakePresets.json 中合并和/或覆盖不同配置预设的标志

转载 作者:行者123 更新时间:2023-12-02 18:21:48 27 4
gpt4 key购买 nike

我的 CMakePresets.json 中有两个配置预设,我想将继承 configurePresets (gcc) 的标志与另一个预设 (gcc-arm-embedded) 合并

这是一个简化的版本:

  "configurePresets": [
{
"name": "gcc",
"hidden": true,
"cacheVariables": {
"CMAKE_CXX_FLAGS": "-Wall -Wextra",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "gcc-arm-embedded",
"hidden": true,
"inherits": ["gcc"],
"cacheVariables": {
"CMAKE_CXX_FLAGS": "-ffunction-sections -fdata-sections",
"CMAKE_EXE_LINKER_FLAGS": "-mcpu=cortex-m7 -mthumb",
"CMAKE_BUILD_TYPE": "MinSizeRel"
}
},
{
"name": "embedded",
"inherits": ["gcc", "gcc-arm-embedded"]
}
]

问题是,如果我使用预设 embedded,生成的 CMAKE 标志是:

CMAKE_CXX_FLAGS: "-Wall -Wextra"
CMAKE_EXE_LINKER_FLAGS: "-mcpu=cortex-m7 -mthumb",
CMAKE_BUILD_TYPE: "Release"

我的目标是:

CMAKE_CXX_FLAGS: "-Wall -Wextra -ffunction-sections -fdata-sections"
CMAKE_EXE_LINKER_FLAGS: "-mcpu=cortex-m7 -mthumb",
CMAKE_BUILD_TYPE: "MinSizeRel"

这是否可以通过 CMakePresets 实现?

最佳答案

首先,将inherits条目简化为:

"inherits": ["gcc-arm-embedded"]

解析顺序从左到右,从不覆盖,但由于 gcc-arm-embedded 已经继承自 gcc,因此无需同时指定两者。这样你就可以做到这一点:

CMAKE_CXX_FLAGS: "-ffunction-sections -fdata-sections"
CMAKE_EXE_LINKER_FLAGS: "-mcpu=cortex-m7 -mthumb",
CMAKE_BUILD_TYPE: "MinSizeRel"

现在让我们添加一个基本预设:

    {
"name": "base",
"hidden": true,
"cacheVariables": {
"CMAKE_CXX_FLAGS": "$env{CXX_WARNINGS} $env{CXX_OPT}",
"CMAKE_BUILD_TYPE": "Release"
}
},

然后让 gcc 继承自 base 并删除 cacheVariables 部分。相反,设置:

      "environment": {
"CXX_WARNINGS": "-Wall -pedantic"
}

并在 gcc-arm-embedded 中另外设置:

      "environment": {
"CXX_OPT": "-ffunction-sections -fdata-sections"
}

最终结果是这样的:

{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 21,
"patch": 0
},
"configurePresets": [
{
"name": "base",
"hidden": true,
"cacheVariables": {
"CMAKE_CXX_FLAGS": "$env{CXX_WARNINGS} $env{CXX_OPT}",
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "gcc",
"inherits": ["base"],
"hidden": true,
"environment": {
"CXX_WARNINGS": "-Wall -pedantic"
}
},
{
"name": "gcc-arm-embedded",
"hidden": true,
"inherits": ["gcc"],
"cacheVariables": {
"CMAKE_EXE_LINKER_FLAGS": "-mcpu=cortex-m7 -mthumb",
"CMAKE_BUILD_TYPE": "MinSizeRel"
},
"environment": {
"CXX_OPT": "-ffunction-sections -fdata-sections"
}
},
{
"name": "embedded",
"inherits": ["gcc-arm-embedded"]
}
]
}

关于c++ - 在 CMakePresets.json 中合并和/或覆盖不同配置预设的标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70782546/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com