How to Get CMake to Use Default Compiler on System

2019-06-20 09:29发布

问题:

Currently, I invoke CMake from my build directory as follows:

CXX="/opt/gcc-4.8/bin/g++" cmake ..

to get CMake to use this particular compiler. Otherwise it uses the operating system default compiler.

My PATH has "/opt/gcc-4.8/bin" in front of everything else. So, instead of prepending the environmental variable is there way to specify in the "`CMakeLists.txt" file to use the default g++ on the path?

回答1:

CMake honors the setting of the PATH environment variable, but gives preference to the generic compiler names cc and c++. To determine which C compiler will be used by default under UNIX by CMake, run:

$ which cc

To determine the default C++ compiler, run:

$ which c++

If you generate a symbolic link c++ in /opt/gcc-4.8/bin which points to /opt/gcc-4.8/bin/g++, CMake should use GCC 4.8 by default.



回答2:

The location of cc rather than c++ determines which c++ cmake is going to use. So for example, if you have /usr/local/bin/c++ but /usr/local/bin/cc, cmake would still pickup /usr/bin/c++, not /usr/local/bin/c++. In this case, creating a symbolic link at /usr/local/bin/cc pointing to /usr/local/bin/gcc would have cmake to use /usr/local/bin/c++.

Another approach would be to explicitly set the language of your project to C++:

project(foo CXX)