Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get CMake to Use Default Compiler on System PATH?

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?

like image 981
Jeet Avatar asked Apr 18 '13 04:04

Jeet


People also ask

How do I select a compiler in CMake?

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.

What compiler does CMake use by default?

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.

Where is CMake compiler located?

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.

How do I change my default CMake generator?

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.


2 Answers

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.

like image 108
sakra Avatar answered Oct 07 '22 10:10

sakra


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)

like image 41
York Avatar answered Oct 07 '22 09:10

York