1
votes

I use modules on RHEL5 and have various versions of compilers/binutils located on my machine. As such I end up defining environment variables that point to my tools and update paths accordingly so the old tools that shipped with RHEL5 are out of the picture.

Is there a simple method to have cmake load a corresponding environment variable?

For example in my environment:

CMAKE_CXX_COMPILER=/some/other/compiler
CMAKE_LINKER=/some/other/linker

Is there a way to have cmake grab these without passing them as arguments via the commandline?

The following didnt work for me in my CMakeLists.txt

SET(CMAKE_CXX_COMPILER, $ENV{CMAKE_CXX_COMPILER})

And not surprisingly the following also didnt work:

IF($ENV{CMAKE_CXX_COMPILER})
    SET(CMAKE_CXX_COMPILER, $ENV{CMAKE_CXX_COMPILER})
    MESSAGE("CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER}")
ENDIF()

Maybe it is a syntax issue or not the correct place to update such a cmake variable? It does work when I pass via the commandline (e.g. -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}), but I dont want to do it that way.

Thanks

1
The comma should not be there (in either of the cases). Also, compiler setup is a specific beast in CMake. You'll find $ENV{XYZ} works just fine normally. - Angew is no longer proud of SO

1 Answers

2
votes

Never mind. It was a syntax error: SET(CMAKE_CXX_COMPILER, $ENV{CMAKE_CXX_COMPILER}) shouldn't have had a comma. The correct syntax is:

SET(CMAKE_CXX_COMPILER $ENV{CMAKE_CXX_COMPILER})