Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way in CMake to require GCC version 4+?

Tags:

gcc

cmake

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.

like image 351
RishiD Avatar asked Jan 12 '09 15:01

RishiD


People also ask

Does CMake work with gcc?

CMake supports an extensive list of compilers, including: Apple Clang, Clang, GNU GCC, MSVC, Oracle Developer Studio, and Intel C++ Compiler.

How do I specify gcc?

For example, specifying --program-prefix=foo- would result in ' gcc ' being installed as /usr/local/bin/foo-gcc . Appends suffix to the names of programs to install in bindir (see above). For example, specifying --program-suffix=-3.1 would result in ' gcc ' being installed as /usr/local/bin/gcc-3.1 .

How does CMake know which compiler to use?

As the linker is invoked by the compiler driver, CMake needs a way to determine which compiler to use to invoke the linker. This is calculated by the LANGUAGE of source files in the target, and in the case of static libraries, the language of the dependent libraries.


2 Answers

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

#if __GNUC__ != 4
#error
#endif
int main() { return 0; }
like image 147
JesperE Avatar answered Sep 23 '22 06:09

JesperE


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")
  ...
...
like image 25
cmaker Avatar answered Sep 20 '22 06:09

cmaker