getenv()表示定义的环境变量只有在Eclipse中运行时才未定义

getenv() says a defined environment variable is undefined only when running in Eclipse

本文关键字:Eclipse 运行时 未定义 表示 定义 环境变量 getenv      更新时间:2023-10-16

UPDATE: Eclipse是问题所在——它在控制台工作。但是我仍然希望它能在Eclipse中工作。

我的printenv输出包含以下行:

PROTCAD3DIR=/home/brent/Desktop/protCAD

但是从我试图运行的程序(在Eclipse中),我得到输出:

Environment variable PROTCAD3DIR undefined.
Please set it properly and re-execute the program.

我在源代码中搜索此消息并找到1个结果:

string PCGeneralIO::getEnvironmentVariable(const string& _evname)
{
    const char* convEVName = _evname.c_str();
    char* pEVString = getenv(convEVName);
    if (pEVString == 0)
    {   cout << "Environment variable " << _evname << " undefined." << endl;
        cout << "Please set it properly and re-execute the program." <<     endl;             
        exit(1);
    }               
    string EVstring = charToString(pEVString);    
    return EVstring;
}

我很确定这是在调用上面的函数:

string evname = "PROTCAD3DIR";
string path = PCGeneralIO::getEnvironmentVariable(evname);

那么是什么导致getenv()发现它是未定义的呢?

检查您的程序的Eclipse "Run Configuration"中的"Environment"选项卡。该列表应为空,并选中"追加环境到本机环境"。

Edit如果没有帮助,那么很可能Eclipse没有使用丢失的变量启动。通过使用/usr/bin/printenv作为程序创建新的"外部工具配置"来检查这一点。启动这个外部工具并检查输出。如果没有提到缺少的变量,那么您必须精确地指定

我在一个文件中添加了定义和导出。配置或类似的东西)。

摘自你的评论:-)