How to get CMake to use the default compiler on system PATH?
Asked Answered
S

2

7

There is the same question and the answer. The problem is that the answer seems to be wrong (actually is not the answer to the asked question). Can I re-ask the question? The problem:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ whereis gcc
cc: /usr/bin/gcc /usr/lib/gcc /usr/local/bin/gcc /usr/libexec/gcc
$ which gcc
/usr/local/bin/gcc
$ /usr/bin/gcc -v
gcc version 4.1.2
$ /usr/local/bin/gcc -v
gcc version 4.8.4
$ gcc -v
gcc version 4.8.4
$ cmake .
-- The C compiler identification is GNU 4.1.2
...
...

The local GCC version if 4.8.4 and the system's default version is '4.1.2'. All other tool-chains respects the PATH environment variable and use the local (newer) GCC version. All except of CMAKE. Setting CC is not a good idea, because there might be other binary tools which could also be used. Setting CMAKE_PROGRAM_PATH and CMAKE_PREFIX_PATH in the beginning of the script doesn't help with detection of the compilers.
Is there any way to force CMAKE to respect the PATH variable?

Symonds answered 27/4, 2015 at 18:1 Comment(1)
It's a workaround, but you can also specify the path to your compiler in your cmake configuration, see here.Tieck
N
10

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.

Anyway, to force CMake to use the default compilers on the system path, add the following code to the beginning of your outermost CMakeLists.txt.

find_program(CMAKE_C_COMPILER NAMES $ENV{CC} gcc PATHS ENV PATH NO_DEFAULT_PATH)
find_program(CMAKE_CXX_COMPILER NAMES $ENV{CXX} g++ PATHS ENV PATH NO_DEFAULT_PATH)
...
project (Foo C CXX)

The find_program calls must occur before the call to project or enable_language.

Nevus answered 27/4, 2015 at 19:40 Comment(1)
Thanks, I am an idiot. The only notice - it should be "find_program(CMAKE_C_COMPILER NAMES $ENV{CC} gcc PATHS ENV PATH NO_DEFAULT_PATH)" With this modification it works exactly like other toolchains - respects PATH but prefers CC. It will be the answer for topic's question. Could you change your answer so I could accept it?Symonds
C
6

CMake respects the environment variables CC and CXX. You can export or just set them for cmake.

CC=gcc CXX=g++ cmake .....

Note that this only works in empty build directories. Changing the compiler on existing directories does not work like this.

Cranmer answered 28/4, 2015 at 6:17 Comment(1)
It's not the answer for the topic's question. There are at least four ways to set C/C++ compiler for CMAKE but don't make it working as other tools. And it's not a good idea to set such things up outside the CMAKE-script because it lowers portability - the user might want to specify it's own compiler in those variables.Symonds

© 2022 - 2025 — McMap. All rights reserved.