10

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.

Lenar Hoyt
  • 5,971
  • 6
  • 49
  • 59
  • 2
    Just use `-DCMAKE_CXX_COMPILER` and don't mess around with shell variables. – usr1234567 Jun 13 '16 at 11:54
  • I think what you are searching for is something like `set_directory_properties(PROPERTIES RULE_LAUNCH_COMPILE "bash proxy_script_cc.sh")` or global variables [`CMAKE__COMPILER_LAUNCHER`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_LAUNCHER.html) – Florian Jun 13 '16 at 13:12
  • @Florian The `cc_args.py` script wraps the compiler to extract information for auto-completion. – Lenar Hoyt Jun 13 '16 at 13:52
  • 1
    For me, CMake ignores `ENV{CC}` on Windows but not CentOS. In both cases there are no spaces in the path to GCC. – Qwertie Dec 02 '18 at 05:56

2 Answers2

11

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

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
4

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

usr1234567
  • 21,601
  • 16
  • 108
  • 128