target_compile_options() 仅用于 C++ 文件?

2023-08-27C/C++开发问题
0

本文介绍了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++ 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6