Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clion or cmake does not see environment variable

I'm using CLion IDE, Cmake and trying to write Hello world using CERN ROOT library.

CMakeLists.txt:

message(STATUS $ENV{ROOTSYS})

~/.bashrc:

export ROOTSYS="$HOME/tools/root-build/"

During build in CLion $ENV{ROOTSYS} is empty by some reason. But $ENV{PATH} returns correct $PATH.

What I did wrong?

like image 285
A.Sizov Avatar asked Jun 06 '16 16:06

A.Sizov


People also ask

How do I view environment variables in Cmake?

Operator to read environment variables. Use the syntax $ENV{VAR} to read environment variable VAR . To test whether an environment variable is defined, use the signature if(DEFINED ENV{<name>}) of the if() command.

How do I set environment variables in CLion?

Environment variables You can pass additional environment variables to CMake generation and build via the Environment field of the CMake Settings dialog (navigate to Settings / Preferences | Build, Execution, Deployment | CMake). or pressing Shift+Enter , and set the Include system environment variables checkbox.

How do you set the path in CLion?

Change the location of IDE directories From the main menu, select Help | Edit Custom Properties. Specify paths with forward slashes /, including Windows paths (for example, C:/idea/system). After you restart CLion, it will use the new location of the corresponding directory.

How do I use Cmake variables?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake . You can unset entries in the Cache with unset(... CACHE) .


3 Answers

Variables from .bashrc aren't passed.

Go to File -> Settings -> Build,Execution,Deployment

For Clion 2017.2+

enter image description here enter image description here


For old Clion

enter image description here

Click Pass system and...

enter image description here


If you want to read environment variable in C++ runtime e.g. using std::getenv then it won't work as we added environment variable for CMAKE not for runtime.

You can add such variable: enter image description here

And then in your code:

std::filesystem::path getRootConfigPath()
{
    // std::getenv can return nullptr and this is why we CAN'T assign it directly to std::string
    const char* path = std::getenv("TEST_CONFIG_DIR");
    gcpp::exception::fail_if_true(
        path == nullptr, WHERE_IN_FILE, "No such environment variable: ${TEST_CONFIG_DIR}");

    gcpp::exception::fail_if_true(std::string_view{path}.empty(),
                                  WHERE_IN_FILE,
                                  "Missing ${TEST_CONFIG_DIR} environment variable");

    const std::filesystem::path testConfigDir{path};
    gcpp::exception::fail_if_false(std::filesystem::exists(testConfigDir) &&
                                       std::filesystem::is_directory(testConfigDir),
                                   WHERE_IN_FILE,
                                   "Invalid ${TEST_CONFIG_DIR} dir:" + testConfigDir.string());
    return testConfigDir;
}

Source of gcpp::exception::fail_if_true


Other way to do this in more friendly way when running unit tests is add this variable to template.

So whenever you click: enter image description here

Such variable will be there already.

enter image description here

like image 119
Dawid Avatar answered Sep 19 '22 16:09

Dawid


From CLion developers FAQ:

Q: How to pass environment variables and parameters to CMake in CLion?

A: The best way is to use Preferences/Settings | Build, Execution, Deployment | CMake dialog.

As for .bashrc file, it is only used by bash. CLion doesn't need to use bash for run configuration process.

like image 23
Tsyvarev Avatar answered Sep 19 '22 16:09

Tsyvarev


On Ubuntu 17.04, you can set a permanent environment variable by modifying

    /etc/enviornment

[I assume you can do this in other versions of Linux, but I provide the version of the system that I am using.]

For example, I am compiling test cases that assume that ${GOOGLE_MOCK} has been set. I added the following to my /etc/environment file, and now I don't have to rewrite all of my CMakeLists.txt files:

    GOOGLE_MOCK=/usr/local/src/googletest/googlemock
    GOOGLE_TEST_HOME=/usr/local/src/googletest/googletest

Clion just became much more useable.

like image 36
Gardener Avatar answered Sep 17 '22 16:09

Gardener