开发者

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 &&am开发者_C百科p; GCC_MINOR >= 3)


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


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)


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜