如何在CMake中从COMPILE_FLAGS迁移到target_compile_options? [英] How do I migrate from COMPILE_FLAGS to target_compile_options in CMake?
问题描述
我在CMakeLists.txt中有以下代码来为我的脚本项目设置一些编译器和链接器标志:
I have the following code in my CMakeLists.txt to set some compiler and linker flags for my emscripten project:
set_target_properties(prolog_bfs PROPERTIES COMPILE_FLAGS "-s USE_BOOST_HEADERS=1 -s DISABLE_EXCEPTION_CATCHING=0")
set_target_properties(prolog_bfs PROPERTIES LINK_FLAGS "--bind --emrun -s USE_BOOST_HEADERS=1 -s DISABLE_EXCEPTION_CATCHING=0")
这工作得很好,并且我的编译器会按需使用选项进行调用(我仍然想知道em ++之后的空格是从哪里来的,但这不是问题):
This works perfectly fine and my compiler gets called with the options as it should (I still wonder where the spaces after em++ are coming from though, but this is not an issue):
em++ -s USE_BOOST_HEADERS=1 -s DISABLE_EXCEPTION_CATCHING=0 -std=gnu++17 -o xy.o -c xy.cpp
但是, COMPILE_FLAGS
和 LINK_FLAGS
是
However, COMPILE_FLAGS
and LINK_FLAGS
are deprecated, so I want to migrate to the new/recommended approach of using target_compile_options()
and target_link_options()
instead.
因此,我已经这样更改了我的CMakeLists.txt:
Thus, I have changed my CMakeLists.txt like so:
target_compile_options(prolog_bfs PUBLIC -s USE_BOOST_HEADERS=1;-s DISABLE_EXCEPTION_CATCHING=0)
target_link_options(prolog_bfs PUBLIC --bind;--emrun;-s USE_BOOST_HEADERS=1;-s DISABLE_EXCEPTION_CATCHING=0)
我知道target _ * _ options函数需要用分号分隔标志,而我这样做了.除此之外,我看不到任何其他主要差异.
I understand that the target_*_options function requires to separate flags with a semicolon, which I did. Apart from that, I don't see any other major differences.
使用这些更改来构建我的项目将得到如下所示的编译器:
Building my project with these changes will get the compiler called like this:
em++ -s USE_BOOST_HEADERS=1 DISABLE_EXCEPTION_CATCHING=0 -std=gnu++17 -o xy.o -c xy.cpp
请注意,第二个标记之前的 -s
丢失了.我不明白为什么它消失了.有趣的是,第一个留在那里.
Note that that the -s
before the second flag is missing. I don't understand why it disappears. Interestingly, the first one stays there.
如何在不丢失 -s
的情况下将最初的CMakeLists.txt代码转换为现代方法?
How do I transform my initial CMakeLists.txt code into the modern approach without losing the -s
?
推荐答案
默认情况下,CMake 重复数据消除编译和链接选项.在文档中明确声明了 target_compile_options
命令.此外,文档还建议使用 SHELL:
前缀,以避免由于以下原因而破坏组:
By default, CMake de-duplicates compile and link options. This is explicitly stated in the documentation for target_compile_options
command. Also, the documentation suggests to use SHELL:
prefix for avoid breaking groups because of that:
用于目标的最终一组编译或链接选项是通过累积来自当前目标的选项及其依赖项的使用要求而构建的.选项集将被重复删除,以避免重复.重复数据删除步骤虽然有益于单个选项,但可以拆分选项组.例如,
-D A -DB
变为-DAB
.可以使用类似shell的引号和SHELL:
前缀指定一组选项.删除SHELL:
前缀,并使用separate_arguments()
UNIX_COMMAND
模式解析其余的选项字符串.例如,"SHELL:-DA""SHELL:-DB"
变为-DA-DB
.
The final set of compile or link options used for a target is constructed by accumulating options from the current target and the usage requirements of its dependencies. The set of options is de-duplicated to avoid repetition. While beneficial for individual options, the de-duplication step can break up option groups. For example,
-D A -D B
becomes-D A B
. One may specify a group of options using shell-like quoting along with aSHELL:
prefix. TheSHELL:
prefix is dropped, and the rest of the option string is parsed using theseparate_arguments()
UNIX_COMMAND
mode. For example,"SHELL:-D A" "SHELL:-D B"
becomes-D A -D B
.
也就是说,您可以指定
target_compile_options(prolog_bfs PUBLIC "SHELL:-s USE_BOOST_HEADERS=1" "SHELL:-s DISABLE_EXCEPTION_CATCHING=0")
这篇关于如何在CMake中从COMPILE_FLAGS迁移到target_compile_options?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!