<tfoot id='udU23'></tfoot>

        <legend id='udU23'><style id='udU23'><dir id='udU23'><q id='udU23'></q></dir></style></legend>
          <bdo id='udU23'></bdo><ul id='udU23'></ul>

      1. <small id='udU23'></small><noframes id='udU23'>

        <i id='udU23'><tr id='udU23'><dt id='udU23'><q id='udU23'><span id='udU23'><b id='udU23'><form id='udU23'><ins id='udU23'></ins><ul id='udU23'></ul><sub id='udU23'></sub></form><legend id='udU23'></legend><bdo id='udU23'><pre id='udU23'><center id='udU23'></center></pre></bdo></b><th id='udU23'></th></span></q></dt></tr></i><div id='udU23'><tfoot id='udU23'></tfoot><dl id='udU23'><fieldset id='udU23'></fieldset></dl></div>

        使用 CMAKE 检查编译器中是否启用了 c++11 功能

        时间:2023-08-27
          <i id='Alsc2'><tr id='Alsc2'><dt id='Alsc2'><q id='Alsc2'><span id='Alsc2'><b id='Alsc2'><form id='Alsc2'><ins id='Alsc2'></ins><ul id='Alsc2'></ul><sub id='Alsc2'></sub></form><legend id='Alsc2'></legend><bdo id='Alsc2'><pre id='Alsc2'><center id='Alsc2'></center></pre></bdo></b><th id='Alsc2'></th></span></q></dt></tr></i><div id='Alsc2'><tfoot id='Alsc2'></tfoot><dl id='Alsc2'><fieldset id='Alsc2'></fieldset></dl></div>
            <tbody id='Alsc2'></tbody>
          • <bdo id='Alsc2'></bdo><ul id='Alsc2'></ul>
          • <legend id='Alsc2'><style id='Alsc2'><dir id='Alsc2'><q id='Alsc2'></q></dir></style></legend>
          • <tfoot id='Alsc2'></tfoot>

              <small id='Alsc2'></small><noframes id='Alsc2'>

                • 本文介绍了使用 CMAKE 检查编译器中是否启用了 c++11 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 CMake 开发一个项目.我的代码包含 constexpr 方法,在 Visual Studio 2015 中允许,但在 Visual Studio 2013 中不允许.

                  I'm developing a project with CMake. My code contains constexpr methods, that are allowed in Visual Studio 2015, but not in Visual Studio 2013.

                  如果指定的编译器支持该功能,我如何检查 CMakeLists.txt ?我在 CMake 文档中看到 CMAKE_CXX_KNOWN_FEATURES,但我不明白如何使用它.

                  How can I check in the CMakeLists.txt if the feature is supported by the specified compiler? I've seen in CMake documentation CMAKE_CXX_KNOWN_FEATURES, but I didn't understand how to use it.

                  推荐答案

                  您可以使用 target_compile_features 需要 C++11(/14/17) 特性:

                  You can use target_compile_features to require a C++11(/14/17) feature:

                  target_compile_features(target PRIVATE|PUBLIC|INTERFACE feature1 [feature2 ...])
                  

                  feature1 是 <代码>CMAKE_CXX_KNOWN_FEATURES.例如,如果你想在你的公共 API 中使用 constexpr,你可以使用:

                  add_library(foo ...)
                  target_compile_features(foo PUBLIC cxx_constexpr)
                  

                  <小时>

                  您还应该查看 WriteCompilerDetectionHeader 模块,它允许将特性检测为选项,并在编译器不支持某些特性时提供向后兼容性实现:


                  You should also take a look at the WriteCompilerDetectionHeader module which allows to detect features as options, and provides a backward compatibility implementation for some features if the compiler does not support them:

                  write_compiler_detection_header(
                      FILE foo_compiler_detection.h
                      PREFIX FOO
                      COMPILERS GNU MSVC
                      FEATURES cxx_constexpr cxx_nullptr
                  )
                  

                  如果关键字constexpr可用,这里将生成一个文件foo_compiler_detection.h,其中定义了FOO_COMPILER_CXX_CONSTEXPR:

                  Here a file foo_compiler_detection.h will be generated with FOO_COMPILER_CXX_CONSTEXPR defined if the keyword constexpr is available:

                  #include "foo_compiler_detection.h"
                  
                  #if FOO_COMPILER_CXX_CONSTEXPR
                  
                  // implementation with constexpr available
                  constexpr int bar = 0;
                  
                  #else
                  
                  // implementation with constexpr not available
                  const int bar = 0;
                  
                  #endif
                  

                  此外,如果当前编译器存在该功能,FOO_CONSTEXPR 将被定义并扩展为 constexpr.否则为空.

                  Moreover, FOO_CONSTEXPR will be defined and will expand to constexpr if the feature exists for the current compiler. It will be empty otherwise.

                  FOO_NULLPTR 将被定义并扩展为 nullptr 如果当前编译器存在该功能.否则它将扩展为兼容性实现(例如 NULL).

                  FOO_NULLPTR will be defined and will expand to nullptr if the feature exists for the current compiler. It will expand to a compatibility implementation otherwise (e.g. NULL).

                  #include "foo_compiler_detection.h"
                  
                  FOO_CONSTEXPR int bar = 0;
                  
                  void baz(int* p = FOO_NULLPTR);
                  

                  请参阅 CMake 文档.

                  这篇关于使用 CMAKE 检查编译器中是否启用了 c++11 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么不是命令“cmake".生成makefile? 下一篇:cmake 在 opencv c++ 项目中不起作用

                  相关文章

                  <legend id='0f9pv'><style id='0f9pv'><dir id='0f9pv'><q id='0f9pv'></q></dir></style></legend><tfoot id='0f9pv'></tfoot>

                  <small id='0f9pv'></small><noframes id='0f9pv'>

                  • <bdo id='0f9pv'></bdo><ul id='0f9pv'></ul>
                      <i id='0f9pv'><tr id='0f9pv'><dt id='0f9pv'><q id='0f9pv'><span id='0f9pv'><b id='0f9pv'><form id='0f9pv'><ins id='0f9pv'></ins><ul id='0f9pv'></ul><sub id='0f9pv'></sub></form><legend id='0f9pv'></legend><bdo id='0f9pv'><pre id='0f9pv'><center id='0f9pv'></center></pre></bdo></b><th id='0f9pv'></th></span></q></dt></tr></i><div id='0f9pv'><tfoot id='0f9pv'></tfoot><dl id='0f9pv'><fieldset id='0f9pv'></fieldset></dl></div>