仅用于C ++文件的target_compile_options()? [英] target_compile_options() for only C++ files?
问题描述
是否可以仅对C ++文件使用 target_compile_options()
?我想将其用于作为其他应用程序依赖项的目标,以便该库可以将其编译器标志传播到这些应用程序。但是,有些标志,例如 -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 ++文件中,但这不会(自动)
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)
这篇关于仅用于C ++文件的target_compile_options()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!