gpt4 book ai didi

cmake - 为什么 CMake 为 add_compile_options 和 target_compile_options 拆分参数的方式不同?

转载 作者:行者123 更新时间:2023-12-03 16:49:08 24 4
gpt4 key购买 nike

我正在尝试在 CMake 项目中启用警告。使用 target_compile_options类似于 this answer 中的建议工作正常:

target_compile_options(mytarget PRIVATE $<$<CXX_COMPILER_ID:GNU,Clang>:-Wall -Wextra -pedantic>)

我有很多目标,我想将这些设置应用于所有目标。所以,我尝试使用 add_compile_options反而:
add_compile_options($<$<CXX_COMPILER_ID:GNU,Clang>:-Wall -Wextra -pedantic>)

但是,当我使用这个时,我看到编译器通过 "\$<1:-Wall" -Wextra "-pedantic>" ,就好像在扩展生成器表达式之前发生了空间 split 。这会混淆编译器并且构建失败。

我试着引用 add_compile_commands 的论点:
add_compile_options("$<$<CXX_COMPILER_ID:GNU,Clang>:-Wall -Wextra -pedantic>")

但是当我这样做时,编译器会传递一个 "-Wall -Wextra -pedantic"参数,这也会混淆它并导致构建失败。

我可以像这样“分发”生成器表达式:
add_compile_options(
$<$<CXX_COMPILER_ID:GNU,Clang>:-Wall>
$<$<CXX_COMPILER_ID:GNU,Clang>:-Wextra>
$<$<CXX_COMPILER_ID:GNU,Clang>:-pedantic>
)

这工作正常,并解决了我的直接问题......

...但是,问题仍然存在:是什么导致了 add_compile_optionstarget_compile_options 不同的工作方式这里?它们的字符串拆分语义不应该相同吗?

最佳答案

也就是说,因为 add_compile_options 用于向 COMPILE_OPTIONS 添加编译选项列表,分号分隔的列表,而 add_target_options INTERFACE_COMPILE_OPTIONS 添加选项数据类型 property .
你离得太近了!用分号替换空格后,您的代码会立即生效:

add_compile_options("$<$<CXX_COMPILER_ID:GNU,Clang>:-Wall;-Wextra;-pedantic>")
是的!我还认为,这种行为是不一致的,而且 add_compile_options应该更类似于 target_compile_options .也许其他人可以进一步阐述它的历史,但是当我发现 [^_]COMPILE_OPTIONS[^_] 时,我的好奇心得到了满足。比 [^_]INTERFACE_COMPILE_OPTIONS[^_] 在代码中出现的频率要高得多确实:这意味着 121/40 在 d231f429f10e9!我认为,它已经存在了更长时间,改变它的实现方式很可能会破坏某些用户的现有构建链,而没有人想要这样。

关于cmake - 为什么 CMake 为 add_compile_options 和 target_compile_options 拆分参数的方式不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61433216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com