简体   繁体   中英

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?

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 eg 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. Hope this helps someone else!

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.

在 .profile 而不是 .bashrc 中获取变量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM