我想實作與 C 等效,#define foo "bar"但我不希望該值"bar"成為我的代碼存盤庫的一部分,并且該值將取決于構建是 Debug 還是 Release。
我怎樣才能做到這一點?
注意:我喜歡在 CMakeLists.txt 中使用以下內容的想法,add_compile_definitions(foo="bar")但我不知道如何"bar"從 Qt Creator 構建設定中提供。
大概我會添加一個鍵/值對,但我會放什么?

uj5u.com熱心網友回復:
我建議您使用以下方式設定定義target_compile_definitions:
target_compile_definitions(myexe PUBLIC foo="bar")
要根據它是發布還是除錯將其設定為不同的東西,使用生成器運算式將使這些選項在使用多配置生成器時具有可移植性:
target_compile_definitions(myexe PUBLIC
$<$<CONFIG:Debug>:foo="bar">
$<$<CONFIG:Release>:baz="bat">
)
如果您真的不希望該bar選項出現在您的存盤庫中,包括 CMake 檔案,也有一些方法可以定義它。
您可以將其作為命令列選項的一部分發送。為此,您可以使用命令定義該命令列選項option,然后使用預設發送命令列引數:
option(MYPROJECT_MY_OPTION "Activate the definitions" OFF)
if(MYPROJECT_MY_OPTION)
target_compile_definitions(myexe PUBLIC
$<$<CONFIG:Debug>:foo=bar">
$<$<CONFIG:Release>:baz="bat">
)
endif()
然后創建一些組態檔:
{
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 22,
"patch": 0
},
"configurePresets": [
{
"name": "cmake-pedantic",
"hidden": true,
"warnings": {
"dev": false,
"deprecated": true,
"uninitialized": true,
"unusedCli": true,
"systemVars": false
},
"errors": {
"dev": false,
"deprecated": true
}
},
{
"name": "generator-ninja",
"hidden": true,
"generator": "Ninja Multi-Config",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_DEBUG_POSTFIX": "d",
"CMAKE_MAKE_PROGRAM": "ninja"
}
},
{
"name": "dev",
"displayName": "Development",
"description": "Development preset",
"inherits": ["generator-ninja"]
},
{
"name": "dev-with-option",
"displayName": "Development",
"description": "Development preset",
"binaryDir": "${sourceDir}/build-with-option",
"inherits": ["generator-ninja"],
"cacheVariables": {
"MYPROJECT_MY_OPTION": true
}
}
],
"buildPresets": [
{
"name": "dev-debug",
"displayName": "Debug",
"description": "Build with debug informations",
"configuration": "Debug",
"configurePreset": "dev"
},
{
"name": "dev-relwithdebinfo",
"displayName": "RelWithDebInfo",
"description": "Build with debug informations and optimizations enabled",
"configuration": "RelWithDebInfo",
"configurePreset": "dev"
},
{
"name": "dev-release",
"displayName": "Release",
"description": "Build with optimizations enabled",
"configuration": "Release",
"configurePreset": "dev"
},
{
"name": "dev-with-option-debug",
"displayName": "Debug",
"description": "Build with debug informations",
"configuration": "Debug",
"configurePreset": "dev-with-option"
},
{
"name": "dev-with-option-relwithdebinfo",
"displayName": "RelWithDebInfo",
"description": "Build with debug informations and optimizations enabled",
"configuration": "RelWithDebInfo",
"configurePreset": "dev-with-option"
},
{
"name": "dev-with-option-release",
"displayName": "Release",
"description": "Build with optimizations enabled",
"configuration": "Release",
"configurePreset": "dev-with-option"
}
],
"testPresets": [
{
"name": "base-test",
"hidden": true,
"output": {
"outputOnFailure": true
},
"execution": {
"noTestsAction": "error",
"stopOnFailure": true
}
},
{
"name": "dev-debug",
"displayName": "Debug",
"configuration": "Debug",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-relwithdebinfo",
"displayName": "RelWithDebInfo",
"configuration": "RelWithDebInfo",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-release",
"displayName": "Release",
"configuration": "Release",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-with-option-debug",
"displayName": "Debug",
"configuration": "Debug",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-with-option-relwithdebinfo",
"displayName": "RelWithDebInfo",
"configuration": "RelWithDebInfo",
"configurePreset": "dev",
"inherits": "base-test"
},
{
"name": "dev-with-option-release",
"displayName": "Release",
"configuration": "Release",
"configurePreset": "dev",
"inherits": "base-test"
}
]
}
這將允許 IDE 在 和 之間進行選擇dev,dev-with-option還允許 IDE 選擇除錯或發布配置。
uj5u.com熱心網友回復:
你需要一個兩步的程序:
- 添加 Qt Creator 構建設定以創建CMake 快取變數。
- 在 CMakeLists.txt 中,在命令中使用快取變數。
對于您的示例:
- 在 Qt Creator 構建設定中,
- 單擊添加>字串設定鍵
MYPROJECT_FOO和值bar - 或單擊批量編輯...并添加
-DMYPROJECT_FOO:STRING=bar
- 在 CMakeLists 中,
target_compile_definitions(mytarget PUBLIC foo="${MYPROJECT_FOO}")
(正如 Guillaume Racicot 所建議的,最好將定義應用于單個目標,而不是整個專案,這將是add_compile_definitions(foo="${MYPROJECT_FOO}")。)
另請參閱 Qt Creator 檔案CMake 構建配置:修改變數值。
感謝 Guillaume Racicot 更深入地解釋了如何使用 CMake 快取變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/518498.html
上一篇:QtFontMetricsboundingrectvsGeometryrect
下一篇:Json將鍵更改為值
