PopcornKing
PopcornKing

Reputation: 1380

CMAKE use environment variables without passing them as commandline arguments?

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

Upvotes: 1

Views: 3353

Answers (1)

PopcornKing
PopcornKing

Reputation: 1380

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})

Upvotes: 2

Related Questions