我想实现C++ #define foo "bar"的等效功能,但我不希望"bar"值成为我的代码库的一部分,该值将取决于构建版本是调试版本还是发布版本。我该怎么做?注意:我喜欢在CMakeLists.txt中使用以下代码add_compile_definitions(foo="bar")的想法,但我不知道如何从Qt Creator构建设置中提供"bar"。我可能会添加一个键/值对,但我会放什么呢?
#define foo "bar"
"bar"
add_compile_definitions(foo="bar")
kmynzznz1#
我建议您使用target_compile_definitions来设置定义:
target_compile_definitions
target_compile_definitions(myexe PUBLIC foo="bar")
要根据发布或调试将其设置为不同的内容,使用generator expressions将使这些选项在使用多配置生成器时可移植:
target_compile_definitions(myexe PUBLIC $<$<CONFIG:Debug>:foo="bar"> $<$<CONFIG:Release>:baz="bat"> )
如果您真的不希望bar选项出现在您的存储库(包括CMake文件)中,也有一些方法可以定义它。您可以将其作为命令行选项的一部分发送。为此,您可以使用option命令定义该命令行选项,然后使用预设值发送命令行参数:
bar
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选择调试或发布配置。
dev
dev-with-option
ovfsdjhp2#
您需要两个步骤:1.添加一个Qt Creator构建设置以创建一个 *CMake缓存变量 *。1.在CMakeLists.txt中,请在命令内该高速缓存变数。例如:1.在Qt Creator构建设置中,
MYPROJECT_FOO
-DMYPROJECT_FOO:STRING=bar
1.在CMakeLists中,target_compile_definitions(mytarget PUBLIC foo="${MYPROJECT_FOO}")(As Guillaume Racicot建议,最好将定义应用于单个目标,而不是整个项目,即add_compile_definitions(foo="${MYPROJECT_FOO}")。)另请参阅Qt Creator文档CMake构建配置:修改变量值。
target_compile_definitions(mytarget PUBLIC foo="${MYPROJECT_FOO}")
add_compile_definitions(foo="${MYPROJECT_FOO}")
2条答案
按热度按时间kmynzznz1#
我建议您使用
target_compile_definitions
来设置定义:要根据发布或调试将其设置为不同的内容,使用generator expressions将使这些选项在使用多配置生成器时可移植:
如果您真的不希望
bar
选项出现在您的存储库(包括CMake文件)中,也有一些方法可以定义它。您可以将其作为命令行选项的一部分发送。为此,您可以使用
option
命令定义该命令行选项,然后使用预设值发送命令行参数:然后创建一些配置文件:
这将允许IDE在
dev
和dev-with-option
之间进行选择,还允许IDE选择调试或发布配置。ovfsdjhp2#
您需要两个步骤:
1.添加一个Qt Creator构建设置以创建一个 *CMake缓存变量 *。
1.在CMakeLists.txt中,请在命令内该高速缓存变数。
例如:
1.在Qt Creator构建设置中,
MYPROJECT_FOO
,将值设置为bar
-DMYPROJECT_FOO:STRING=bar
1.在CMakeLists中,
target_compile_definitions(mytarget PUBLIC foo="${MYPROJECT_FOO}")
(As Guillaume Racicot建议,最好将定义应用于单个目标,而不是整个项目,即
add_compile_definitions(foo="${MYPROJECT_FOO}")
。)另请参阅Qt Creator文档CMake构建配置:修改变量值。