Why does CMake ignore exported CXX and CC environm

2019-05-03 19:18发布

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.

标签: cmake
2条回答
虎瘦雄心在
2楼-- · 2019-05-03 19:36

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

查看更多
爷、活的狠高调
3楼-- · 2019-05-03 19:37

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

查看更多
登录 后发表回答