16

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?

Asen Christov
  • 848
  • 6
  • 21
A.Sizov
  • 171
  • 1
  • 1
  • 7

5 Answers5

18

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

Gelldur
  • 11,187
  • 7
  • 57
  • 68
  • 1
    Any chance you can update this to match the 2017.2 version of CLion? It doesn't look anything like these screen shots. – Brannon Aug 09 '17 at 21:05
  • Is it possible to specify RC file for a shell that's used to run CMake? There are cases when environment variables are not so good - for example, I install Conan (package manager) to ./local and then add this directory to the PATH (via .bashrc), but I still cannot run Conan from the CMake. – isnullxbh Jul 18 '21 at 12:32
13

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.

Community
  • 1
  • 1
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
1

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Gardener
  • 2,591
  • 1
  • 13
  • 22
0

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.

xxks-kkk
  • 2,336
  • 3
  • 28
  • 48
0

Source your variables in .profile not .bashrc