How to use setenv() to export a variable in c++?
From the setenv()
manual entry:
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.
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.
whether the environment variable exported in c function will be available in parent shell?
Read about getenv(3) and setenv(3) and putenv(3)
But environment variables (actually, the entire virtual address space and process state) are local to their process (e.g. your shell) and changing it does not affect those in other processes (except future child processes obtained with fork(2)). Hence changing an environment variable -or any other memory location, or the working directory, or file descriptors, etc...- in a child process don't affect its parent process (or any other existing one). See also fork(2) and execve(2).
You should consider other kinds of inter-process communication such as pipe(7)-s or socket(7)-s (and perhaps shm_overview(7) with sem_overview(7)). You might want to have some event loop above poll(2).
Read intro(2), syscalls(2) and some book like ALP.
BTW, most existing Unix shells are free software. So you can download and study their source code. And you could strace(1) them. See also this.
Set environment variables in C
I'm going to make a wild guess here, but the normal reason that these functions appear to not work is not because they don't work, but because the user doesn't really understand how environment variables work. For example, if I have this program:
int main(int argc, char **argv)
{
putenv("SomeVariable=SomeValue");
return 0;
}
And then I run it from the shell, it won't modify the shell's environment - there's no way for a child process to do that. That's why the shell commands that modify the environment are builtins, and why you need to source
a script that contains variable settings you want to add to your shell, rather than simply running it.
Set local environment variables in C++
NAME
putenv - change or add an environment variable
SYNOPSIS
#include <stdlib.h>
int putenv(char *string);
DESCRIPTION
The putenv() function adds or changes the value of environment
variables. The argument string is of the form name=value. If name does
not already exist in the environment, then string is added to the
environment. If name does exist, then the value of name in the
environment is changed to value. The string pointed to by string becomes
part of the environment, so altering the string changes the environment.
On Win32 it's called _putenv I believe.
See SetEnvironmentVariable also if you're a fan of long and ugly function names.
Related Topics
How to Know If a Type Is a Specialization of Std::Vector
Std::Vector of Std::Vectors Contiguity
When Extending a Padded Struct, Why Can't Extra Fields Be Placed in the Tail Padding
Union for Uint32_T and Uint8_T[4] Undefined Behavior
Why Calling Main() Is Not Allowed in C++
Determine If Map Contains a Value for a Key
Why Does This Call the Default Constructor
Same Random Numbers Every Time I Run the Program
Cast Vector<T> to Vector<Const T>
Arithmetic Right Shift Gives Bogus Result
How to Connect a Signal to a Static Slot Without a Receiver Instance
Program Is Generating Same Random Numbers on Each Run
Spirit-Qi: How to Write a Nonterminal Parser