Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check gcc minor in cmake

Is it possible to check the minor version number of GCC in cmake?

I want to do something like this:

If (GCC_MAJOR >= 4 && GCC_MINOR >= 3)
like image 495
Karl von Moor Avatar asked Oct 30 '10 11:10

Karl von Moor


People also ask

What does the if command do in CMake?

Any non-integer version component or non-integer trailing part of a version component effectively truncates the string at that point. The if command was written very early in CMake's history, predating the $ {} variable evaluation syntax, and for convenience evaluates variables named by its arguments as shown in the above signatures.

Is it possible to modify the cmakelist in the newer version?

For the sake of specify the newer version of gcc, I tried to modify the CMakeList.txt by adding some like the following: which reminds me it is a bad idea, even though I had backed up a copy. BUT, export is a ideal approach. that is exporting system variables of CXX and CC to CMake’s cache before cmake:

What version of the compiler does CMake_CXX_compiler_version provide?

CMake correctly recognises the version and provides 6.99.1 in CMAKE_CXX_COMPILER_VERSION. However, when compiler.version = 6.99.1 is specified in the associated profile (and white-listed in the settings.yml ), configuring the project fails with the following error:

Should I export system variables from CXX to CMake before CMake?

BUT, export is a ideal approach. that is exporting system variables of CXX and CC to CMake’s cache before cmake: The export only needs to be done once, the first time you configure the project, then those values will be read from the CMake cache.


4 Answers

Use if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.2) as mentioned by onqtam. This obsolete answer was back from the 2.6 CMake days.

You could run gcc -dumpversion and parse the output. Here is one way to do that:

if (CMAKE_COMPILER_IS_GNUCC)
    execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
                    OUTPUT_VARIABLE GCC_VERSION)
    string(REGEX MATCHALL "[0-9]+" GCC_VERSION_COMPONENTS ${GCC_VERSION})
    list(GET GCC_VERSION_COMPONENTS 0 GCC_MAJOR)
    list(GET GCC_VERSION_COMPONENTS 1 GCC_MINOR)

    message(STATUS ${GCC_MAJOR})
    message(STATUS ${GCC_MINOR})
endif()

That would print "4" and "3" for gcc version 4.3.1. However you can use CMake's version checking syntax to make life a bit easier and skip the regex stuff:

execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
                OUTPUT_VARIABLE GCC_VERSION)
if (GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3)
        message(STATUS "Version >= 4.3")
endif()

like image 57
richq Avatar answered Oct 14 '22 14:10

richq


Since CMake 2.8.10 there are the CMAKE_C_COMPILER_VERSION and CMAKE_CXX_COMPILER_VERSION variables exactly for this purpose so you can do this:

if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.2)
like image 38
onqtam Avatar answered Oct 14 '22 12:10

onqtam


Combining the 2 other answers, you can check the specific gcc version as follows:

if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.1)
    ...
endif()
like image 22
Steve Lorimer Avatar answered Oct 14 '22 14:10

Steve Lorimer


However, there is an argument, -dumpfullversion that provides the full version string.

gcc -dumpfullversion

should get what you want. Still backward compatibility is broken in gcc 7.

like image 36
Basu Avatar answered Oct 14 '22 14:10

Basu