如何使用 CMake 检测编译器对 C++11 的支持
问题描述
有没有办法让 CMake 自动检测编译器是否支持 C++11?
最好在 CMake 运行期间通知用户代码将无法编译,因为编译器不支持 C++11.目前我设置了 C++11 标志.但是,如果编译器不支持它,用户会在 CMake 运行期间收到编译错误而不是错误.
完美的应该是类似于 find_package() 的东西.但是,我没有找到提供所需功能的任何模块或函数.
另外,如果能够检测编译器是否需要标志
>std=c++0x
或 std=c++11
,那就太好了.
有什么可用的东西还是我需要自己开发?
以下是我目前使用的一些代码,但它仅适用于 GNU'c GCC 编译器.如果有更通用的解决方案就好了.
如果您有 CMake 3.1.0 或更高版本,您可以检测哪些 C++ 功能你的 C++ 编译器支持
但通常您不需要在 CMake 脚本中使用 CMake 变量 CMAKE_CXX_COMPILE_FEATURES.相反,有两种方法可以告诉 CMake 在哪个 C++ 标准下编译你的 C++ 文件,或者通过明确指定 C++ 标准或通过指定所需的 C++ 功能并让 CMake 引入 C++ 标准.CMake 将确保使用正确的命令行标志(例如 -std=c++11)调用 C++ 编译器.
1.明确指定 C++ 标准
您可以通过设置 CMake 属性来明确指定 C++ 标准CXX_STANDARD和CXX_STANDARD_REQUIRED 用于您的 CMake 目标.
2.指定所需的 C++ 特性,让 CMake 引入 C++ 标准
您可以使用 CMake 命令 target_compile_features 指定在 CMake 目标中使用的 C++ 功能.从这个列表中,CMake 将引入要使用的 C++ 标准.CMake 全局属性 CMAKE_CXX_KNOWN_FEATURES 列出了 C++ 功能您可以从中选择.
例如,这个文件名为 main.cc 的 C++ 程序利用了 C++11 的特性:cxx_strong_enums、cxx_constexpr、cxx_auto_type
这个 CMakeLists.txt 文件将构建它
Is there a way to let CMake detect automatically if a compiler supports C++11 or not?
As it would be nice to inform the users during the CMake run that the code will not compile as the compiler does not support C++11. At the moment I set the C++11 flags. However, if a compiler does not support it the user gets compile errors instead of an error during the CMake run.
Perfect would be something that works like find_package()
. However, I have not found any module or function which provides the functionality needed.
Additional it would be nice to have the feature to detect if the compiler needs the flags std=c++0x
or std=c++11
.
Is there something available or do I need to develop this on my own?
Below is some code I use so far, however it works only with GNU'c GCC compilers. It would be nice if there would be a more general solution.
If you have CMake version 3.1.0 or later you can detect what C++ features your C++ compiler supports
But normally you don't need to use the CMake variable CMAKE_CXX_COMPILE_FEATURES in your CMake scripts. Instead there are two ways of how to tell CMake under which C++ standard your C++ files should be compiled, either by specifying the C++ standard explicitly or by specifying the required C++ features and let CMake induce the C++ standard. CMake will make sure the C++ compiler is invoked with the correct command line flags (e.g. -std=c++11).
1. Specifying the C++ standard explicitly
You could specify the C++ standard explicitly, by setting the CMake properties CXX_STANDARD and CXX_STANDARD_REQUIRED for your CMake target.
2. Specifying the required C++ features and let CMake induce the C++ standard
You could use the CMake command target_compile_features to specify the C++ features that are made use of in your CMake target. From this list CMake will induce the C++ standard to be used. The CMake global property CMAKE_CXX_KNOWN_FEATURES lists the C++ features you can choose from.
For example, this C++ program with the filename main.cc makes use of the C++11 features: cxx_strong_enums, cxx_constexpr, cxx_auto_type
This CMakeLists.txt file would build it
这篇关于如何使用 CMake 检测编译器对 C++11 的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!