C++ Cmake:如何通过命令行将字符串附加到变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23806160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cmake : how to append string to the variable via command line?
提问by kubivan
In my CMakeList.txt i can do the following thing:
在我的 CMakeList.txt 我可以做以下事情:
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -new -flags -here")
Is it possible to to the same thing via command line? Like:
是否可以通过命令行做同样的事情?喜欢:
cmake.exe -DCMAKE_CXXFLAGS+= -new -flags
回答by Svalorzen
I'm not sure whether you can directly add options from the command line, but you can use an additional variable to store them and merge it at the end. Like the following:
我不确定您是否可以直接从命令行添加选项,但您可以使用附加变量来存储它们并在最后合并它。像下面这样:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_FLAGS}")
And then call cmake as following:
然后调用 cmake 如下:
cmake -DMY_FLAGS="-new -flags"
回答by DrumM
First of all, keep your hands off CMAKE_CXX_FLAGS
! Use target_compile_options
:
首先,把手放开CMAKE_CXX_FLAGS
!使用target_compile_options
:
target_compile_options(<YOUR_TARGET> PRIVATE ${MY_FLAGS})
To expand other lists use list
:
要扩展其他列表,请使用list
:
list(APPEND <YOUR_LIST> <ITEM_TO_ADD>)
回答by David K
If you don't like the command-line syntax specified by @Svalorzen, you could write a script that interprets its command-line arguments the way you like and converts them to something you can put on the cmake.exe command line. If portability is a concern, you could write the script in a language such as Perl (generally available on Unix-like platforms and can be installed on Windows).
如果您不喜欢@Svalorzen 指定的命令行语法,您可以编写一个脚本,以您喜欢的方式解释其命令行参数,并将它们转换为您可以放在 cmake.exe 命令行上的内容。如果考虑可移植性,您可以使用诸如 Perl 之类的语言编写脚本(通常在类 Unix 平台上可用,并且可以安装在 Windows 上)。
If all this is going to buy you is an alternative solution to the problem you described, however, I would recommend just using the answer from @Svalorzen.
但是,如果所有这些都为您购买的是您所描述问题的替代解决方案,那么我建议您只使用@Svalorzen 的答案。