2

I'm trying to work around CLion's inability to handle environment variables.

All of my CMake files start with lines like this:

cmake_minimum_required(VERSION 2.8.4)
include( $ENV{PROJECT_ROOT}/src/global_settings.cmake )
AssureOutOfSourceBuilds()

CLion does not know how to read the environment variable which provides the real path to the global include. As a result, CLion just completely fails to find anything outside of the current directory, as well as throwing errors.

Is there some way to specify a default value for an environment variable in CMake's cache? I believe CLion correctly reads the CMake cache.

Any other suggestions on how to get around CLion's current weakness? Thanks.

4

3 回答 3

4

It turns out that Clion has some unusual behavior with respect to CMake's cache. At least in all of my tests, Clion kept resetting or clearing a cache variable (PROJECT_ROOT, above) whenever I set it, so @sakra's solution was not reliable. I have still voted it the answer to be fair to him for all of his effort.

However, I finally found a solution for this problem, here: https://youtrack.jetbrains.com/issue/CPP-188#comment=27-899992

It is a trick to actually set the environment variable when Clion calls CMake. Here are the contents of that link above:

Here's an ugly work-around for people who need to pass environment or set cache variables via -D, in EAP build 140.1221.2. In Preferences -> Build, Execution, Deployment -> Toolchains, change the CMake executable from the bundled CMake executable to a shell script you define which calls an installed actual CMake executable.

Using the Bash shell, it might look like this:

#!/bin/sh
export SOMEPATH=/path/you/need 
/usr/local/bin/cmake "$@"
于 2015-01-20T17:42:58.750 回答
2

Provided that CLion correctly reads the CMake cache, you can give the following workaround a try:

if (DEFINED ENV{PROJECT_ROOT})
    set (PROJECT_ROOT "$ENV{PROJECT_ROOT}" CACHE PATH "project root" FORCE)
endif()
include( ${PROJECT_ROOT}/src/global_settings.cmake )

The set command creates a cache variable with the environment variable's value. The cache variable can then be used as a replacement for $ENV{...} expressions. The FORCE options ensures that the cache variable is kept up to date with the environment variable's value, if it is set at all.

于 2015-01-15T18:56:07.993 回答
2

As suggested here, since CLion 1.0 you can directly pass CMake options in Settings/Preferences - Build, Execution, Deployment - CMake.

CMake options:

于 2017-09-06T10:35:51.103 回答