如何检测c ++ 11支持带有cmake的编译器 [英] How to detect c++11 support of a compiler with cmake
问题描述
有没有办法让cmake自动检测一个编译器是否支持C ++ 11?
由于在cmake运行期间通知用户将很好,代码将无法编译,因为编译器不支持C ++ 11。目前我设置C ++ 11标志,但如果编译器不支持它,用户在cmake运行期间得到编译错误,而不是错误。
完美是 find_package()
的工作,但是我还没有找到任何提供所需功能的模块。
另外,如果编译器需要标志 std = c ++ 0x
或 std = c ++ 11
有没有可用的东西,或者我需要自己开发吗?
这里有些代码我使用到目前为止,但它只适用于GNU gcc编译器。
if(CMAKE_COMPILER_IS_GNUCXX)
execute_process(COMMAND $ { CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
if(GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7)
消息(状态C ++ 11激活)
add_definitions( - std = gnu ++ 11)
elseif(GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3)
消息(警告C ++ 0x激活。如果更新到完全支持C ++ 11的编译器, b $ b add_definitions( - std = gnu ++ 0x)
else()
消息(FATAL_ERROR需要C ++ 11,因此需要一个版本高于4.3的gcc编译器。 )
endif()
else(CMAKE_COMPILER_IS_GNUCXX)
add_definitions( - std = c ++ 0x)
endif(CMAKE_COMPILER_IS_GNUCXX)
解决方案如果您有CMake版本3.1.0或更高版本,您可以检测C ++的特性
C ++编译器支持cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(foobar CXX )
消息(您的C ++编译器支持这些C ++特性:)
foreach(i $ {CMAKE_CXX_COMPILE_FEATURES})
消息($ {i}
但通常您不需要使用CMake变量 CMAKE_CXX_COMPILE_FEATURES 你的CMake脚本。相反,有两种方法可以通过
明确指定C ++标准,或者通过指定所需的C ++特性,让CMake引导C ++标准,来告诉CMake根据哪个C ++标准编译C ++文件。 CMake将确保使用正确的命令行标志(例如-std = c ++ 11)调用C ++编译器。
1。明确指定C ++标准
您可以通过设置CMake属性
CXX_STANDARD
和
CXX_STANDARD_REQUIRED 。$ cat /tmp/src/CMakeLists.txt
project(foobar CXX)
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
add_executable(prog main.cc)
set_property(TARGET prog PROPERTY CXX_STANDARD 11)
set_property(TARGET prog PROPERTY CXX_STANDARD_REQUIRED ON)
$ cat /tmp/src/main.cc
int main(){
return 0;
}
$ mkdir / tmp / build
$ cd / tmp / build
$ cmake / tmp / src
- CXX编译器标识是GNU 4.8.2
- 检查CXX编译器是否正常工作:/ usr / bin / c ++
- 检查CXX编译器是否正常工作:/ usr / bin / c ++ - works
- 检测CXX编译器ABI信息
- 检测CXX编译器ABI信息 - done
- 检测CXX编译特性
- 检测CXX编译特性 - 完成
- 配置完成
- 生成done
- 构建文件已写入:/ tmp / build
$ make VERBOSE = 1 | grep main.cc | grep - -c
/ usr / bin / c ++ -std = gnu ++ 11 -o CMakeFiles / prog.dir / main.cc.o -c /tmp/src/main.cc
$
2。指定所需的C ++特性并让CMake引入C ++标准
您可以使用CMake命令 target_compile_features 以指定在CMake目标中使用的C ++功能。从这个列表CMake将诱使要使用的C ++标准。 CMake全局属性 CMAKE_CXX_KNOWN_FEATURES 列出了C ++的功能您可以选择。
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
message 您的CMake版本支持这些C ++功能:)
get_property(known_features GLOBAL PROPERTY CMAKE_CXX_KNOWN_FEATURES)
foreach(i $ {known_features})
消息($ {i} b endforeach()
例如,此C ++程序的文件名为 main.cc 使用C ++ 11的特性:
cxx_strong_enums , cxx_constexpr , cxx_auto_type#include< cstdlib>
int main(int argc,char * argv []){
枚举类颜色{红,橙,黄,绿,蓝,紫};
constexpr float a = 3.1415f;
auto b = a;
return EXIT_SUCCESS;
}
这个CMakeLists.txt文件会构建它
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(foobar CXX)
add_executable(foobar main.cc)
set need_features
cxx_strong_enums
cxx_constexpr
cxx_auto_type)
target_compile_features(foobar PRIVATE $ {needed_features})
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 get compile errors instead of an error during the cmake run.
Perfect would be something that work like
find_package()
however I have not found any module of 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
orstd=c++11
.Is there something available or did I need to develop this on my own?
Here some code I use so far, however it works only with GNU gcc compilers. It would be nice if there would be a more general solution.
if(CMAKE_COMPILER_IS_GNUCXX) execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION) if (GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7) message(STATUS "C++11 activated.") add_definitions("-std=gnu++11") elseif(GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3) message(WARNING "C++0x activated. If you get any errors update to a compiler which fully supports C++11") add_definitions("-std=gnu++0x") else () message(FATAL_ERROR "C++11 needed. Therefore a gcc compiler with a version higher than 4.3 is needed.") endif() else(CMAKE_COMPILER_IS_GNUCXX) add_definitions("-std=c++0x") endif(CMAKE_COMPILER_IS_GNUCXX)
解决方案If you have CMake version 3.1.0 or later you can detect what C++ features your C++ compiler supports
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) project(foobar CXX) message("Your C++ compiler supports these C++ features:") foreach(i ${CMAKE_CXX_COMPILE_FEATURES}) message("${i}") endforeach()
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.
$ cat /tmp/src/CMakeLists.txt project(foobar CXX) cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) add_executable(prog main.cc) set_property(TARGET prog PROPERTY CXX_STANDARD 11) set_property(TARGET prog PROPERTY CXX_STANDARD_REQUIRED ON) $ cat /tmp/src/main.cc int main() { return 0; } $ mkdir /tmp/build $ cd /tmp/build $ cmake /tmp/src -- The CXX compiler identification is GNU 4.8.2 -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /tmp/build $ make VERBOSE=1 | grep main.cc | grep -- "-c" /usr/bin/c++ -std=gnu++11 -o CMakeFiles/prog.dir/main.cc.o -c /tmp/src/main.cc $
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.
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) message("Your CMake version supports these C++ features:") get_property(known_features GLOBAL PROPERTY CMAKE_CXX_KNOWN_FEATURES) foreach(i ${known_features}) message("${i}") endforeach()
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
#include <cstdlib> int main(int argc, char *argv[]) { enum class Color { Red, Orange, Yellow, Green, Blue, Violet }; constexpr float a = 3.1415f; auto b = a; return EXIT_SUCCESS; }
This CMakeLists.txt file would build it
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) project(foobar CXX) add_executable(foobar main.cc) set(needed_features cxx_strong_enums cxx_constexpr cxx_auto_type) target_compile_features(foobar PRIVATE ${needed_features})
这篇关于如何检测c ++ 11支持带有cmake的编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!