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?
Thanks.
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?
Thanks.
Do like this:
Note it's synopsis as well:
See this link for more details on
setenv()
.From the
setenv()
manual entry:So you should call
or
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.Here the signature for the setenv function
int setenv(const char *envname, const char *envval, int overwrite);
Link : http://pubs.opengroup.org/onlinepubs/009695399/functions/setenv.html
In your case you call it like this:
the last boolean argument indicates if you want to overwrite the value of the environment variables if it already exists.