如果无法满足CMAKE_CXX_STANDARD,如何在CMake中干净地错误?

发布于 2025-02-13 17:42:45 字数 224 浏览 0 评论 0 原文

Cmake提供 CMAKE_CXX_STANDARD 用于指定所需的C ++标准。

set(cmake_cxx_standard 17)

但是,如果您的编译器旧了...说GCC4 ...它仍然会尝试编译来源,并且由于缺少丢失的奇异错误消息,它当然会失败编译器功能。

有没有干净的方法使CMAKE检测到这种缺失的支持并以更明显的方式失败?

CMake provides CMAKE_CXX_STANDARD for specifying the required C++ Standard.

set (CMAKE_CXX_STANDARD 17)

However, if your compiler is old... say gcc4... it will still attempt to compile the sources, and it will of course fail with bizarre error messages, due to missing compiler features.

Is there a clean way to make CMake detect this missing support, and fail in a more obvious way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔纱相望 2025-02-20 17:42:46

使用target_compile_features进行更多控制

,如 kamilcuk 10149169“>他们的答案,您可以检查CXX_STD_17。

此外,您可以在目标上设置需求:

target_compile_features(my_example_library PUBLIC cxx_std_17)

https://cmake.org/cmake/help/help/latest/manual/cmake-compile-features.7.html#requiring-language-standards只有一些目标需要某些标准。同样的语法也可以仅需仅需要特定功能,在

Simpler option for CMAKE_CXX_STANDARD, set CXX_STANDARD_REQUIRED

However, if you're using

# will affect any library/target defined after this
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# also recommended, uses -std=c++17 instead of -std=gnu++17
# if you code is compatible with non-GCC compilers too
# set(CMAKE_CXX_EXTENSIONS OFF)

您会收到一个错误,看起来像:

CMake Error in CMakeLists.txt:
  Target "my_example_library" requires the language dialect "C17" (with
  compiler extensions).  But the current compiler "GNU" does not support
  this, or CMake does not know the flags to enable it.

Using target_compile_features for more control

As KamilCuk mentioned in their answer, you can check for cxx_std_17.

Additionally, you can set the requirement on a target with:

target_compile_features(my_example_library PUBLIC cxx_std_17)

Source https://cmake.org/cmake/help/latest/manual/cmake-compile-features.7.html#requiring-language-standards

This is more verbose, but it allows you to specify that only some targets need certain standards. The same syntax can also be used to require only specific features, full list found in CMAKE_CXX_KNOWN_FEATURES.

Simpler option for CMAKE_CXX_STANDARD, set CXX_STANDARD_REQUIRED

However, if you're using CMAKE_CXX_STANDARD, it's much easier to just set CMAKE_CXX_STANDARD_REQUIRED, e.g.:

# will affect any library/target defined after this
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# also recommended, uses -std=c++17 instead of -std=gnu++17
# if you code is compatible with non-GCC compilers too
# set(CMAKE_CXX_EXTENSIONS OFF)

You'll get an error that looks something like:

CMake Error in CMakeLists.txt:
  Target "my_example_library" requires the language dialect "C17" (with
  compiler extensions).  But the current compiler "GNU" does not support
  this, or CMake does not know the flags to enable it.
怎会甘心 2025-02-20 17:42:46

From https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD .html

请参阅CMAKE-COMPILE-FEATURES(7)手册,以获取有关编译功能和支持编译器列表的信息。

来自 https://cmake.org/cmake/help/help/latest/manual/cmake-compile-features.7.html#manual:Cmake-compile-features(7)

cmake_c_known_features,cmake_cuda_known_features和cmake_cxx_nown_features全局属性包含CMake已知的所有功能,无论编译器对该功能的支持如何。 cmake_c_compile_features,cmake_cuda_compile_features和cmake_cxx_compile_features变量包含所有功能Cmake知道,无论是使用它们的语言标准或编译标志所需的语言标准,

来自 https://cmake.org/cmake/help/help/latest/prop_gbl/cmake_cxx_nown_nown_nown_features.html#prop_gbl:cmake_cxx_features

cxx_std_17

Compiler mode is at least C++ 17.

noreferrer

if ("cxx_std_17" IN_LIST CMAKE_CXX_COMPILE_FEATURES)

From https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html :

See the cmake-compile-features(7) manual for information on compile features and a list of supported compilers.

From https://cmake.org/cmake/help/latest/manual/cmake-compile-features.7.html#manual:cmake-compile-features(7) :

The CMAKE_C_KNOWN_FEATURES, CMAKE_CUDA_KNOWN_FEATURES, and CMAKE_CXX_KNOWN_FEATURES global properties contain all the features known to CMake, regardless of compiler support for the feature. The CMAKE_C_COMPILE_FEATURES, CMAKE_CUDA_COMPILE_FEATURES , and CMAKE_CXX_COMPILE_FEATURES variables contain all features CMake knows are known to the compiler, regardless of language standard or compile flags needed to use them.

From https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html#prop_gbl:CMAKE_CXX_KNOWN_FEATURES :

cxx_std_17

Compiler mode is at least C++ 17.

Check:

if ("cxx_std_17" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文