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++ 文件?
基础教程推荐
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- c++ STL设置差异 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
