Clion或cmake看不到环境变量 [英] Clion or cmake does not see environment variable

查看:1715
本文介绍了Clion或cmake看不到环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CLion IDE,Cmake,并尝试使用CERN ROOT库编写 Hello world .

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/"

由于某些原因,在CLion $ENV{ROOTSYS}中构建期间为空.但是$ENV{PATH}返回正确的$PATH.

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

我做错了什么?

推荐答案

.bashrc中的变量没有通过.

Variables from .bashrc aren't passed.

转到文件->设置-> 构建,执行,部署

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

点击通过"系统,然后...

Click Pass system and...

如果您想在C ++运行时中读取环境变量,例如使用 std :: getenv 然后 它不起作用,因为我们为 CMAKE 添加了环境变量,而不是为运行时添加了环境变量.

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:

然后在您的代码中:

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;
}

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.

这篇关于Clion或cmake看不到环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆