Reputation: 5087
My OS is centos which has a default gcc in path /usr/bin/gcc
. But it is old, I need a new version of gcc. So I install a new version in a new path /usr/local/bin/gcc
.
But when I run cmake
, it still uses the old version gcc path(/usr/bin/gcc
) . How can I specify the gcc to new path(/usr/local/bin/gcc
).
I have tried to overwrite /usr/bin/gcc
with /usr/local/bin/gcc
, but it not work.
Upvotes: 183
Views: 353053
Reputation: 21
RE Guillaume's answer:
Why yes, you can just swap swap CMAKE_C_COMPILER
and CMAKE_CXX_COMPILER
, but you need to do that before "initializing" the project:
set(CMAKE_C_COMPILER <path/to/c/compiler>)
set(CMAKE_CXX_COMPILER <path/to/c++/compiler>)
project(PROJECT_NAME)
CMake actually performs those checks (in this case it actually doesn't since the existence of those variables indicates to it that it had already detected a working compiler, so no need to check it) when you do the project "initialization", so if you do the swapping before that, it checks your newly swapped variables.
Though, better to set CC
and CXX
environment variables:
set(ENV{CC} <path/to/compiler>)
set(ENV{CXX} <path/to/compiler>)
project(PROJECT_NAME)
That way CMake also performs compiler checks.
Not sure this is the "correct" way to do it, but works for me ("computing" the compiler location I need when running CMake script, and swapping the default one for it).
Upvotes: 2
Reputation: 10971
Do not overwrite CMAKE_C_COMPILER
, but export CC
(and CXX
) before calling cmake:
export CC=/usr/local/bin/gcc
export CXX=/usr/local/bin/g++
cmake /path/to/your/project
make
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.
UPDATE: longer explanation on why not overriding CMAKE_C(XX)_COMPILER
after Jake's comment
I recommend against overriding the CMAKE_C(XX)_COMPILER
value for two main reasons: because it won't play well with CMake's cache and because it breaks compiler checks and tooling detection.
When using the set
command, you have three options:
Let's see what happens for the three possible calls to set
:
Without cache
set(CMAKE_C_COMPILER /usr/bin/clang)
set(CMAKE_CXX_COMPILER /usr/bin/clang++)
When doing this, you create a "normal" variable CMAKE_C(XX)_COMPILER
that hides the cache variable of the same name. That means your compiler is now hard-coded in your build script and you cannot give it a custom value. This will be a problem if you have multiple build environments with different compilers. You could just update your script each time you want to use a different compiler, but that removes the value of using CMake in the first place.
Ok, then, let's update the cache...
With cache
set(CMAKE_C_COMPILER /usr/bin/clang CACHE PATH "")
set(CMAKE_CXX_COMPILER /usr/bin/clang++ CACHE PATH "")
This version will just "not work". The CMAKE_C(XX)_COMPILER
variable is already in the cache, so it won't get updated unless you force it.
Ah... let's use the force, then...
Force cache
set(CMAKE_C_COMPILER /usr/bin/clang CACHE PATH "" FORCE)
set(CMAKE_CXX_COMPILER /usr/bin/clang++ CACHE PATH "" FORCE)
This is almost the same as the "normal" variable version, the only difference is your value will be set in the cache, so users can see it. But any change will be overwritten by the set
command.
Breaking compiler checks and tooling
Early in the configuration process, CMake performs checks on the compiler: Does it work? Is it able to produce executables? etc. It also uses the compiler to detect related tools, like ar
and ranlib
. When you override the compiler value in a script, it's "too late", all checks and detections are already done.
For instance, on my machine with gcc as default compiler, when using the set
command to /usr/bin/clang
, ar
is set to /usr/bin/gcc-ar-7
. When using an export before running CMake it is set to /usr/lib/llvm-3.8/bin/llvm-ar
.
Upvotes: 314
Reputation: 22246
CMAKE_<LANG>_COMPILER
path without triggering a reconfigureI wanted to compile with an alternate compiler, but also pass -D options on the command-line which would get wiped out by setting a different compiler. This happens because it triggers a re-configure. The trick is to disable the compiler detection with NONE
, set the paths with FORCE
, then enable_language
.
project( sample_project NONE )
set( COMPILER_BIN /opt/compiler/bin )
set( CMAKE_C_COMPILER ${COMPILER_BIN}/clang CACHE PATH "clang" FORCE )
set( CMAKE_CXX_COMPILER ${COMPILER_BIN}/clang++ CACHE PATH "clang++" FORCE )
enable_language( C CXX )
The more sensible choice is to create a toolchain file.
set( CMAKE_SYSTEM_NAME Darwin )
set( COMPILER_BIN /opt/compiler/bin )
set( CMAKE_C_COMPILER ${COMPILER_BIN}/clang CACHE PATH "clang" )
set( CMAKE_CXX_COMPILER ${COMPILER_BIN}/clang++ CACHE PATH "clang++" )
Then you invoke Cmake with an additional flag
cmake -D CMAKE_TOOLCHAIN_FILE=/path/to/toolchain_file.cmake ...
Upvotes: 5
Reputation: 76799
This not only works with cmake
, but also with ./configure
and make
:
./configure CC=/usr/local/bin/gcc CXX=/usr/local/bin/g++
Which is resulting in:
checking for gcc... /usr/local/bin/gcc
checking whether the C compiler works... yes
Upvotes: 2
Reputation: 3385
This question is quite old but still turns up on Google Search. The accepted question wasn't working for me anymore and seems to be aged. The latest information about cmake is written in the cmake FAQ.
There are various ways to change the path of your compiler. One way would be
Set the appropriate
CMAKE_FOO_COMPILER
variable(s) to a valid compiler name or full path on the command-line usingcmake -D
. For example:cmake -G "Your Generator" -D CMAKE_C_COMPILER=gcc-4.2 -D CMAKE_CXX_COMPILER=g++-4.2 path/to/your/source
instead of gcc-4.2
you can write the path/to/your/compiler
like this
cmake -D CMAKE_C_COMPILER=/path/to/gcc/bin/gcc -D CMAKE_CXX_COMPILER=/path/to/gcc/bin/g++ .
Upvotes: 53
Reputation: 20306
An alternative solution is to configure your project through cmake-gui, starting from a clean build directory. Among the options you have available at the beginning, there's the possibility to choose the exact path to the compilers
Upvotes: 3
Reputation: 107
Export should be specific about which version of GCC/G++ to use, because if user had multiple compiler version, it would not compile successfully.
export CC=path_of_gcc/gcc-version
export CXX=path_of_g++/g++-version
cmake path_of_project_contain_CMakeList.txt
make
In case project use C++11 this can be handled by using -std=C++-11
flag in CMakeList.txt
Upvotes: 4
Reputation: 249542
Set CMAKE_C_COMPILER
to your new path.
See here: http://www.cmake.org/Wiki/CMake_Useful_Variables
Upvotes: 8