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?

4 Answers 4

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.

4
  • 1
    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.
    – Ruslan
    Commented Jun 29, 2016 at 14:16
  • 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.
    – Zitrax
    Commented Dec 10, 2018 at 21:36
  • See answer by @Steve Lorimer below: it seems to be the correct answer to the question.
    – scrutari
    Commented Feb 19, 2020 at 13:49
  • Although the question isn't the one I really had, this answer gave me the answer that I needed. Commented Apr 29, 2020 at 20:14
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()
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()
-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)."

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.
    – pacmaninbw
    Commented Dec 12, 2022 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.