Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does CMake ignore exported CXX and CC environment variables?

Tags:

cmake

I am running a CMake (3.4.3) like this as explained in the CMake FAQ's:

export CC="cc_args.py $PWD/../bin/gcc"
export CXX="cc_args.py $PWD/../bin/g++"
cmake -DCMAKE_BUILD_TYPE=Debug ..

However when I print CMAKE_CXX_COMPILER and CMAKE_C_COMPILER it still points to the system's default compilers in /usr/bin. It only works when I explicitly read-in the environment variables like this:

IF (NOT $ENV{CC} STREQUAL "")
  SET(CMAKE_C_COMPILER $ENV{CC})
ENDIF ()
IF (NOT $ENV{CXX} STREQUAL "")
  SET(CMAKE_CXX_COMPILER $ENV{CXX})
ENDIF ()

But even then the building fails with this message:

/bin/sh: 1: /home/peterg/bin/cc_args.py /home/peterg/Code/build/../bin/g++: not found

However I am certain that all paths are correct since executing just the path between the two colons outputs this as expected:

g++: fatal error: no input files
compilation terminated.

Update:

It seems the compiling process does not like spaces in the compiler paths. I've now created two scripts (one for GCC and one for CC) which wrap the commands and propagate the arguments and that seems to work. But it still seems I am doing something fundamentally wrong because CMake would also not accept the exported CC=proxy_script_cc.sh and GCC=proxy_script_gcc.sh variables without spaces by itself.

like image 342
Lenar Hoyt Avatar asked Jun 13 '16 10:06

Lenar Hoyt


People also ask

What is CC and CXX environment variables?

Typically the CC environment variable is used for the C compiler. However it appears the Makefile s here use it as the C++ compiler, which generally won't work. Would be better to switch to using CXX , which is the environment variable used to specify the C++ compiler.

What does CXX mean CMake?

XX stands for "++" (each X is like a "plus" rotated by 45°), CXX stands for "C++".

Does CMake use environment variables?

This is a CMake Environment Variable. Its initial value is taken from the calling process environment. Preferred executable for compiling C language files. Will only be used by CMake on the first configuration to determine C compiler, after which the value for CC is stored in the cache as CMAKE_C_COMPILER .

How does CMake define an environment variable?

Use the syntax $ENV{VAR} to read environment variable VAR . See also the set() command to set ENV{VAR} .


2 Answers

Turning my comment into an answer

Problem

I've given you code a try and could reproduce your problem

CMake Error at [...]/cmake-3.5/Modules/CMakeDetermineCXXCompiler.cmake:56 (message):
  Could not find compiler set in environment variable CXX:

  cc_args.py [... PWD ...]/../bin/g++.

If I look at CMakeDetermineCXXCompiler.cmake code and at get_filename_component() documentation, it just means that it didn't find cc_args.py in "the system search path" or relative to your binary output directory.

Solution

So it does work when you give a full path or a relative path to your binary output dir with something like

export CC="../cc_args.py ../bin/gcc"
export CXX="../cc_args.py ../bin/g++"

Alternative

CMake does allow to define "launcher scripts" e.g. with CMAKE_<LANG>_COMPILER_LAUNCHER

$ cmake -DCMAKE_BUILD_TYPE=Debug 
        -DCMAKE_C_COMPILER_LAUNCHER=../cc_args.py 
        -DCMAKE_CXX_COMPILER_LAUNCHER=../cc_args.py 
        ..

References

  • How to Use CCache with CMake?
  • Save and reprint warnings for successfully-compiled files on subsequent builds?
like image 198
Florian Avatar answered Oct 13 '22 05:10

Florian


Pass -DCMAKE_CXX_COMPILER=<path/to/compiler> to your CMake call. That's less error prone compared to fiddling with shell variables.

like image 20
usr1234567 Avatar answered Oct 13 '22 05:10

usr1234567