Question

I am using some features that are provided in GCC v4+ and would like CMake to find GCC v4 compiler and if it does not find it, return an error stating GCC v4 is required.

Anyone have any modules / ideas on how to do something like this?

Thanks.

Was it helpful?

Solution

Use the try_compile() command and try to compile the following program

#if __GNUC__ != 4
#error
#endif
int main() { return 0; }

OTHER TIPS

A wholly different (not necessarily better) way to implement a gcc version check would be something like:

if(CMAKE_COMPILER_IS_GNUCXX)
  exec_program(
      ${CMAKE_CXX_COMPILER}
      ARGS                    --version
      OUTPUT_VARIABLE _compiler_output)
  string(REGEX REPLACE ".* ([0-9]\\.[0-9]\\.[0-9]) .*" "\\1"
         gcc_compiler_version ${_compiler_output})
  message(STATUS "C++ compiler version: ${gcc_compiler_version} [${CMAKE_CXX_COMPILER}]")

  if(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
    message(FATAL_ERROR "foobar")
    ...

  if(gcc_compiler_version VERSION_GREATER "4.5.99")
  ...
...

There may soon be a solution in cmake itself, cf. http://cmake.3232098.n2.nabble.com/GCC-compiler-version-td7185771.html

Future releases of cmake may include two variables : CMAKE_C_COMPILER_VERSION and CMAKE_CXX_COMPILER_VERSION.

Halas, I've not seen them yet in 2.8.8 changelog.

I use @cmaker solution for now.

There's also the toolchain file, which is mentioned on the vtk wiki. That lets you specify custom toolchains. It's typically used for cross-compiling, but I've used it before to get other toolchains working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top