如何使用 setenv() 在 C++ 中导出变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17929414/
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
How to use setenv() to export a variable in c++?
提问by return 0
I need to export several variables such that they look like the following in the command line
我需要导出几个变量,使它们在命令行中如下所示
export ROS_HOSTNAME=xxx
How do I use setenv() in c++ to achieve that?
我如何在 C++ 中使用 setenv() 来实现这一点?
Thanks.
谢谢。
回答by π?ντα ?ε?
From the setenv()
manual entry:
从setenv()
手动输入:
SYNOPSIS
#include <stdlib.h> int setenv(const char *envname, const char *envval, int overwrite);
DESCRIPTION
The setenv() function shall update or add a variable in the environment of the calling process. The envname argument points to a string containing the name of an environment variable to be added or altered. The environment variable shall be set to the value to which envval points. The function shall fail if envname points to a string which contains an '=' character. If the environment variable named by envname already exists and the value of overwrite is non-zero, the function shall return success and the environment shall be updated. If the environment variable named by envname already exists and the value of overwrite is zero, the function shall return success and the environment shall remain unchanged.If the application modifies environ or the pointers to which it points, the behavior of setenv() is undefined. The setenv() function shall update the list of pointers to which environ points.
The strings described by envname and envval are copied by this function.
The setenv() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.
RETURN VALUE
Upon successful completion, zero shall be returned. Otherwise, -1 shall be returned, errno set to indicate the error, and the environment shall be unchanged.
概要
#include <stdlib.h> int setenv(const char *envname, const char *envval, int overwrite);
说明
setenv() 函数应在调用进程的环境中更新或添加变量。envname 参数指向一个字符串,其中包含要添加或更改的环境变量的名称。环境变量应设置为 envval 指向的值。如果 envname 指向包含“=”字符的字符串,则该函数将失败。如果由 envname 命名的环境变量已经存在并且 overwrite 的值非零,则函数将返回成功并更新环境。如果 envname 命名的环境变量已经存在,并且 overwrite 的值为零,则函数返回成功,环境保持不变。如果应用程序修改了环境或其指向的指针,则 setenv() 的行为是未定义的。setenv() 函数应更新环境指向的指针列表。
由 envname 和 envval 描述的字符串由该函数复制。
setenv() 函数不需要可重入。不需要可重入的函数不需要是线程安全的。
返回值
成功完成后,应返回零。否则返回-1,设置errno表示错误,环境不变。
So you should call
所以你应该打电话
setenv("ROS_HOSTNAME","xxx",1); // does overwrite
or
或者
setenv("ROS_HOSTNAME","xxx",0); // does not overwrite
for your case. Depends, if you want to overwrite a possibly existing definition.
对于你的情况。视情况而定,如果您想覆盖可能存在的定义。
NOTE:
笔记:
You can't use setenv()
to export variables from your process to the calling process (shell)!
Child processes created with fork, will inherit the current processes environment definitions, thus your changes and additions as well.
您不能使用setenv()
将变量从您的进程导出到调用进程(shell)!使用 fork 创建的子进程将继承当前的进程环境定义,因此您的更改和添加也是如此。
回答by elmadj
Here the signature for the setenv function
这里是 setenv 函数的签名
#include <stdlib.h>
int setenv(const char *envname, const char *envval, int overwrite);
int setenv(const char *envname, const char *envval, int overwrite);
Link : http://pubs.opengroup.org/onlinepubs/009695399/functions/setenv.html
链接:http: //pubs.opengroup.org/onlinepubs/009695399/functions/setenv.html
In your case you call it like this:
在您的情况下,您可以这样称呼它:
setenv("ROS_HOSTNAME", "xxx", true);
the last boolean argument indicates if you want to overwrite the value of the environment variables if it already exists.
最后一个布尔参数指示是否要覆盖环境变量的值(如果它已经存在)。