How to use setenv() to export a variable in c++?

后端 未结 3 551
感情败类
感情败类 2020-12-01 16:36

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()

3条回答
  •  一整个雨季
    2020-12-01 16:53

    Here the signature for the setenv function

    #include 
    

    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:

    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.

提交回复
热议问题