Linux extern char **environ 的定义在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10649273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:22:51  来源:igfitidea点击:

where is the definition of extern char **environ?

clinux

提问by nzomkxia

we can get the environment variable in C like this:

我们可以像这样在 C 中获取环境变量:

extern char **environ;
int main(int argc, char *argv[])
{
    int count = 0;

    printf("\n");
    while(environ[count] != NULL)
   {
         printf("[%s] :: ", environ[count]);
         count++;
   }

   return 0;
}

but where is the defination of environ? I can't find that in unistd.h. and how does it work?

但是环境的定义在哪里?我在 unistd.h 中找不到。它是如何工作的?

采纳答案by Fred Foo

environis defined as a global variable in the Glibc source file posix/environ.c.

environ在 Glibc 源文件中定义为全局变量posix/environ.c

回答by triclosan

man:

男人:

This variable must be declared in the user program, but is declared in the header file unistd.h in case the header files came from libc4 or libc5, and in case they came from glibc and _GNU_SOURCE was defined.

该变量必须在用户程序中声明,但在头文件 unistd.h 中声明,以防头文件来自 libc4 或 libc5,以及它们来自 glibc 并且定义了 _GNU_SOURCE。

回答by Maria Zverina

Have you tried declaring envp as parameter to main?

您是否尝试将 envp 声明为 main 的参数?

int main (int argc, char *argv[], char *envp[])

http://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html#Program-Arguments

http://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html#Program-Arguments