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?
CMake does check for the compiler ids by compiling special C/C++ files. So no need to manually include from Module/Compiler or Module/Platform . This will be automatically done by CMake based on its compiler and platform checks.
As is already written in the answer to the other question, CMake prefers the generic compiler names cc and c++ when searching for the C and C++ compilers. These probably refer to GNU version 4.1 compilers on your system.
Tell CMake where to find the compiler by setting either the environment variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path to the compiler, or to the compiler name if it is in the PATH.
With CMake version 3.15 or later, you can set the CMAKE_GENERATOR environment variable to specify the default generator to be used on your system.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With