target_compile_options() for only C++ files?(target_compile_options() 仅用于 C++ 文件?)
问题描述
是否可以仅将 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++ 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:target_compile_options() 仅用于 C++ 文件?


- Stroustrup 的 Simple_window.h 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- C++ 协变模板 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 从python回调到c++的选项 2022-11-16
- STL 中有 dereference_iterator 吗? 2022-01-01
- 近似搜索的工作原理 2021-01-01