[英]What's the use of the third, environment variable argument to the C++ main()?
I have come to understand that char **envp
is the third argument to main
, and with the help of the code below, I was able to see what it actually contains. 我已经明白
char **envp
是main
的第三个参数,并且在下面的代码的帮助下,我能够看到它实际包含的内容。
int main(int argc, char *argv[], char *env[])
{
int i;
for (i=0 ; env[i] ; i++)
std::cout << env[i] << std::endl;
std::cout << std::endl;
}
My question is: why (in what situations) would programmers need to use this? 我的问题是:为什么(在什么情况下)程序员需要使用它? I have found many explanations for what this argument does, but nothing that would tell me where this is typically used.
我已经找到了这种说法做什么很多解释,但没有什么会告诉我在哪里 ,这是通常使用的。 Trying to understand what kind of real world situations this might be used in.
试图了解这可能会使用哪种现实世界的情况。
It is an array containing all the environmental variables. 它是一个包含所有环境变量的数组。 It can be used for example to get the user name or home directory of current logged in user.
例如,它可用于获取当前登录用户的用户名或主目录。 One situation is, for example, if I want to hold a configuration file in user's home directory and I need to get the PATH;
例如,一种情况是,如果我想在用户的主目录中保存配置文件,我需要获取PATH;
int main(int argc, char* argv[], char* env[]){
std::cout << env[11] << '\n'; //this prints home directory of current user(11th for me was the home directory)
return 0;
}
Equivalent of env
is char* getenv (const char* name) function which is easier to use, for example: 相当于
env
是char * getenv(const char * name)函数,它更易于使用,例如:
std::cout << getenv("USER");
prints user name of current user. 打印当前用户的用户名。
The getenv()
function allows you to find the value of a specific environment variable, but doesn't provide a mechanism to scan over the entire list of environment variables. getenv()
函数允许您查找特定环境变量的值,但不提供扫描整个环境变量列表的机制。 The envp
argument allows you to iterate over the entire list of environment variables, as your demonstration code shows which is simply not feasible using the getenv()
interface. envp
参数允许您遍历整个环境变量列表,因为您的演示代码使用getenv()
接口显示哪些是不可行的。
On POSIX systems, there is a global variable, extern char **environ;
在POSIX系统上,有一个全局变量,
extern char **environ;
, which also points to the environment. ,这也指向环境。 The functions
putenv()
(ancient, non-preferred because it presents memory management problems), setenv()
and unsetenv()
can also manipulate the environment variable list (as defined by environ
). 函数
putenv()
(古代的,非首选的,因为它提出了内存管理问题), setenv()
和unsetenv()
也可以操作环境变量列表(由environ
定义)。 A program can directly modify environ
or the values it points at, but that is not advisable. 程序可以直接修改
environ
或它指向的值,但这是不可取的。
If you are using fork()
and the exec*()
family of functions, unless you use execve()
and specify the environment explicitly, the child process will receive the environment defined by environ
. 如果使用
fork()
和exec*()
系列函数,除非使用execve()
并明确指定环境,子进程将接收environ
定义的environ
。
No header declares environ
— AFAIK, it is the only variable defined by POSIX without a header to declare it. 没有标头声明
environ
- AFAIK,它是POSIX定义的唯一变量,没有标头来声明它。 The C standard recognizes the int main(int argc, char **argv, char **envp)
signature for main()
as a common extension to the standard, documented in Annex J. C标准识别
int main(int argc, char **argv, char **envp)
签名main()
作为共同的扩展标准,附录J.记录
This is typically used to set configuration options or other information for a whole group of programs. 这通常用于为整组程序设置配置选项或其他信息。 Another use is to specify environment settings for a particular machine or user setup.
另一个用途是指定特定机器或用户设置的环境设置。
Well known examples are the PATH
variable that contains the lookup pathes for executables, or the LD_LIBRARY_PATH
variable that contains the pathes where to lookup the shared libraries. 众所周知的示例是包含可执行文件的查找路径的
PATH
变量,或包含查找共享库的路径的LD_LIBRARY_PATH
变量。
env
allows you to access the environment variables . env
允许您访问环境变量 。 It contains an array of strings. 它包含一个字符串数组。 Examples are the users home directory, the configured language scheme, the PATH variable (where to look for directly executable programs?), ...
示例是用户主目录,配置的语言方案, PATH变量 (在哪里直接查找可执行程序?),...
You can also set individual environment variables. 您还可以设置单个环境变量。 For example, if you have testing (learning) and also a production system you deploy your application to.
例如,如果您有测试(学习)以及生产系统,则可以将应用程序部署到。 On one system you could set the variable "MY_APP_MODE=TEST" and on the other system you could specify "MY_APP_MODE=PROD".
在一个系统上,您可以设置变量“MY_APP_MODE = TEST”,在另一个系统上,您可以指定“MY_APP_MODE = PROD”。 So you don't need to deploy different applications to the test and production systems.
因此,您无需将不同的应用程序部署到测试和生产系统。 Your application could determine itself in what environment it is run.
您的应用程序可以在运行的环境中确定自己。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.