问题描述
是否可以仅将 target_compile_options()
用于 C++ 文件?我想将它用于作为其他应用程序依赖项的目标,以便库可以将其编译器标志传播到这些应用程序.但是,某些标志(例如 -std=c++14
)如果与 C 或 ObjC 文件一起使用会导致构建失败.
Is it possible to use target_compile_options()
for only C++ files? I'd like to use it for a target that is uses as a dependency for other applications so that the library can propagate its compiler flags to those apps. However, there are certain flags, such as -std=c++14
, that cause the build to fail if they are used with C or ObjC files.
我已经读到我应该 CXX_FLAGS
而不是只将这些标志添加到 C++ 文件中,但是这不会(自动)通过 cmake 的包系统传播.
I've read that I should CXX_FLAGS
instead to only add those flags to C++ files, however this won't (automatically) propagate through cmake's packages system.
推荐答案
解决方案
您可以使用生成器表达式:
target_compile_options(MyLib PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-std=c++14>)
替代方案
但在这种特殊情况下,更独立于平台的方法是使用 target_compile_features()
.我不确定您使用的是哪个 编译器功能使用,所以以下只是一个例子:
But the more platform independent way of doing it in this particular case would be to use target_compile_features()
. I'm not sure which compiler feature you're using, so the following is only an example:
target_compile_features(MyLib PUBLIC cxx_explicit_conversions)
这篇关于target_compile_options() 仅用于 C++ 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!