Forcing latest gcc available with cmake
I am deploying a small application to several different systems (mac, linux, linux64) where it needs to be compiled. I would like to tell cma开发者_如何学运维ke to the latest gcc available in a particular system. For example, Mac 10.6 has gcc 4.0 and gcc 4.2 (default). But some users have also gcc 4.4 installed through MacPorts (it is not the default). I would like cmake to use gcc44 in this case. In other linux systems, the latest gcc is 4.4 or 4.5 What is the more robust way to achieve this?
Thanks,
H
CMake honors the environment variables CC
and CXX
upon detecting the C and C++ compiler to use. E.g., if those variables point to clang, it will use clang by default:
$ export CC=/usr/bin/clang
$ export CXX=/usr/bin/clang++
$ cmake ..
-- The C compiler identification is Clang
-- The CXX compiler identification is Clang
-- Check for working C compiler: /usr/bin/clang
-- Check for working C compiler: /usr/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++
-- Check for working CXX compiler: /usr/bin/clang++ -- works
...
You could either define these as system wide environment variables pointing to the preferred compilers or write a small shell wrapper script which tests the availability of several compilers and sets the variables accordingly before invoking cmake.
I had to add this to my CMakeLists.txt for it to work:
if($ENV{CXX})
set(CMAKE_CXX_COMPILER $ENV{CXX} CACHE FILEPATH "CXX Compiler")
endif()
if($ENV{CC})
set(CMAKE_CC_COMPILER $ENV{CC} CACHE FILEPATH "CC Compiler")
endif()
Another trick is to do make sure the GCC you want is used is to do something like this before CMake:
export PATH=/path/to/my/gcc:$PATH
精彩评论