• Uncategorized

About cmake : Clion-or-cmake-does-not-see-environment-variable

Question Detail

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?

Question Answer

Variables from .bashrc aren’t passed.

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

For Clion 2017.2+


For old Clion

Click Pass system and…


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:

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:

Such variable will be there already.

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.

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.

One thing you can check is the .gdbinit. Clion on Linux will invoke the gdb, which will read in the .gdbinit. I happen to have set environment LD_LIBRARY_PATH xxx in my .gdbinit file, which will override whatever you set LD_LIBRARY_PATH from shell, whether through direct export or through .bashrc, or from CLion environment variable panel.

Hope this helps.

Source your variables in .profile not .bashrc

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.