How can I add a minimum compiler version requisite?
Asked Answered
W

4

36

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

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

Workbook answered 18/2, 2013 at 9:36 Comment(0)
S
56

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()

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.

Slim answered 18/2, 2013 at 10:51 Comment(4)
While more idiomatic, I've found that testing whether std::regex usage compiles takes time comparable to compilation of the whole (small) project. Sometimes checking version is simpler for developer and faster for the user compiling the project.Stellastellar
There is also target_compile_features() to check for specific compile features. But there are a limited number of features that can be checked with it.Contemplate
See answer by @Steve Lorimer below: it seems to be the correct answer to the question.Devotee
Although the question isn't the one I really had, this answer gave me the answer that I needed.Seraphic
H
25

Starting with CMake 2.8.10 the CMAKE_<LANG>_COMPILER_VERSION variables can be accessed by users to get the compiler version. In previous versions those were only reserved for internal purposes and should not be read by user code. They are also not guaranteed to be set for all languages but C and CXX should definitely be available.

CMake also contains operators for version comparison (VERSION_LESS, VERSION_EQUAL, VERSION_GREATER) that you can use to write your version validation code.

Remember that you will need to find out which compiler you have first and then check for the correct version.

Here is a short sample from one of my projects:

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    # require at least gcc 4.8
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
        message(FATAL_ERROR "GCC version must be at least 4.8!")
    endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
    # require at least clang 3.2
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.2)
        message(FATAL_ERROR "Clang version must be at least 3.2!")
    endif()
else()
    message(WARNING "You are using an unsupported compiler! Compilation has only been tested with Clang and GCC.")
endif()
Hin answered 22/8, 2013 at 0:29 Comment(0)
N
12

You can check the specific gcc version as follows:

if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.1)
    message(FATAL_ERROR "Require at least gcc-5.1")
endif()
Nickens answered 23/8, 2016 at 21:29 Comment(0)
J
-1

There is built-in support nowadays. From the Documentation:

target_compile_features(mylib PUBLIC cxx_std_11)

"In this example, CMake will ensure the compiler is invoked in a mode of at-least C++ 11 (or C++ 14, C++ 17, ...), adding flags such as -std=gnu++11 if necessary. This applies to sources within mylib as well as any dependents (that may include headers from mylib)."

Jeremiah answered 14/1, 2022 at 15:36 Comment(1)
This is partially correct, the compiler version isn't just the standard, example gcc supports C++20, but it requires the version of gcc to be at least 12, gcc 11 doesn't fully support C++20.Rufe

© 2022 - 2024 — McMap. All rights reserved.