如何添加最低编译器版本要求? [英] How can I add a minimum compiler version requisite?

查看:31
本文介绍了如何添加最低编译器版本要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 C++11 创建一个项目,我使用 CMake 作为我的构建系统.

I want to create a project in C++11 and I use CMake as my build system.

如何在 CMake 配置文件中添加最低编译器版本要求?

How can I add a minimum compiler version requisite in the CMake config files?

推荐答案

AFAIK,没有内置支持这样的东西,但你当然可以自己写:

AFAIK, there is no built-in support for something like this, but you could certainly write it yourself:

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "your.required.gcc.version")
    message(FATAL_ERROR "Insufficient gcc version")
  endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "your.required.msvc.version")
    message(FATAL_ERROR "Insufficient msvc version")
  endif()
elseif(...)
# etc.
endif()

但是,我建议您实际上考虑使用特征检测方法.也就是说,使用 try_compile() 来验证编译器是否支持您需要的功能,如果不支持,请使用 FATAL_ERROR.它在 CMake 中更为惯用,并且具有额外的好处,您无需为所有编译器找到合适的最小版本.

However, I suggest you actually consider a feature-detection approach instead. That is, use try_compile() to verify that the compiler supports the features you need, and FATAL_ERROR if it doesn't. It's more idiomatic in CMake, and has the added benefit you don't have to discover the appropriate minimal version for all compilers out there.

这篇关于如何添加最低编译器版本要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆