Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用C+;在Linux中获取环境变量$PATH+;日蚀_C++_Environment Variables - Fatal编程技术网

C++ 使用C+;在Linux中获取环境变量$PATH+;日蚀

C++ 使用C+;在Linux中获取环境变量$PATH+;日蚀,c++,environment-variables,C++,Environment Variables,我试图在Linux中使用一个简单的C++程序获取环境变量$PATH的值,如下所示: #include <iostream> #include <string.h> #include <stdio.h> #include <stdlib.h> int main () { // get PATH using pipe FILE* pip = popen("exec bash -c 'echo $PATH'", "r"); if

我试图在Linux中使用一个简单的C++程序获取环境变量$PATH的值,如下所示:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main ()
{
    // get PATH using pipe
    FILE* pip = popen("exec bash -c 'echo $PATH'", "r");
    if (!pip)
    {
        printf("can not open pipe!");
        return 1;
    }

    char lineversion[600];
    memset (lineversion, 0, sizeof(lineversion));
    if (!fgets(lineversion, sizeof(lineversion), pip))
    {
        printf("fgets error!");
        return 1;
    }

    std::cout << lineversion << std::endl;

    // get PATH using getenv
    char* pPath = getenv ("PATH");
    std::cout << pPath << std::endl;
}
#包括
#包括
#包括
#包括
int main()
{
//使用管道获取路径
FILE*pip=popen(“execbash-c'echo$PATH'”,“r”);
如果(!pip)
{
printf(“无法打开管道!”);
返回1;
}
字符行版本[600];
memset(lineversion,0,sizeof(lineversion));
如果(!fgets(lineversion,sizeof(lineversion,pip))
{
printf(“fgets错误!”);
返回1;
}

进程从创建它的进程继承环境

这就是Linux与许多其他操作系统的工作原理

如果从Eclipse启动程序,该程序将继承Eclipse的环境。
如果从shell启动程序,该程序将继承shell的环境,包括对init文件中的
PATH
的修改


由于Eclipse从启动它的任何进程继承了它的环境,如果您从shell而不是通过桌面GUI启动Eclipse,您应该会看到预期的输出。

如果您不在Eclipse中运行程序,会发生什么情况?您的路径可以通过一些配置文件来更改,无论是对于bash还是对于NETBEANS。请检查您的.profile、 主目录中的.bashrc、.bash配置文件。eclipse和shell之间的路径不同。@Galik:如果没有eclipse,它会正确显示路径值!我已经更新了我的问题。所以eclipse对此负责?这可能是因为eclipse不在bash配置文件上运行