0

I am trying to write a C++ program in CLion and use one self-defined environment variable. The OS is Ubuntu 16.04

Say the environment variable is the $test.

int main (int argc, char **argv){

    std::cout<<getenv("PATH");
    std::cout<<getenv("test");
}

I have set the environment variable in the setting->build...->CMAKE->Environment environment variable set

I am able to print it when build through CMAKE.

message($ENV{test}) 

this is test

But whenbuild and run the compiled code above, only $PATH was print out. It seems the program cannot find the $test variable.

Anyone know how to tackle this problem?

Community
  • 1
  • 1
Peter XU
  • 63
  • 15
  • 1
    Typically IDEs allow separate environments for building and running your program. Set environment variable in your "Run configuration", not in CMake options. – yeputons Feb 06 '17 at 07:22
  • @yeputons thanks! It works after set the environment variable in "run configuration" – Peter XU Feb 06 '17 at 07:33
  • Related question about CMake environment variables: https://stackoverflow.com/a/38874446/1052261 – Gelldur Jan 11 '20 at 13:18

3 Answers3

0

Well, I don't know CLion, but it sure looks like you set the environment variable for use in CMake only. When you run your program, it's simply not set.

Rene
  • 2,466
  • 1
  • 12
  • 18
  • yes! just like @yeputons said, after set the environment variable in the run configuration, it works! Thanks a lot! – Peter XU Feb 06 '17 at 07:34
0

I had the same problem and below steps resolved my issue. Set your variables in

Run->Edit Configurations, Application/Environment Variables

Andre Miras
  • 3,580
  • 44
  • 47
0

If you want to read environment variable in C++ runtime e.g. using std::getenv.

You can add such variable in "Run Configurations" (IMPORTANT not CMake environment variables) 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