我的 CMakeLists.txt 中有以下代码,用于为我的 emscripten 项目设置一些编译器和链接器标志:
旧的工作代码
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++ 之后的空格来自哪里,但这不是问题):
em++ -s USE_BOOST_HEADERS=1 -s DISABLE_EXCEPTION_CATCHING=0 -std=gnu++17 -o xy.o -c xy.cpp
但是,COMPILE_FLAGS
和 LINK_FLAGS
是 deprecated ,因此我想迁移到使用 target_compile_options()
和 target_link_options()
的新/推荐方法。
因此,我像这样更改了我的 CMakeLists.txt:
新方法
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 函数需要用分号分隔标志,我就是这样做的。除此之外,我没有看到任何其他重大差异。
问题
使用这些更改构建我的项目将使编译器像这样调用:
em++ -s USE_BOOST_HEADERS=1 DISABLE_EXCEPTION_CATCHING=0 -std=gnu++17 -o xy.o -c xy.cpp
请注意,第二个标志之前的 -s
丢失了。我不明白为什么它会消失。有趣的是,第一个留在那里。
问题
如何将最初的 CMakeLists.txt 代码转换为现代方法而不丢失 -s
?
最佳答案
默认情况下,CMake 去重复编译和链接选项。 documentation 中明确说明了这一点对于 target_compile_options
命令。此外,文档建议使用 SHELL:
前缀以避免破坏组,因为:
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")
关于c++ - 如何在 CMake 中从 COMPILE_FLAGS 迁移到 target_compile_options?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60552844/