如何使用setenv()在c ++中导出一个变量? [英] How to use setenv() to export a variable in c++?

查看:602
本文介绍了如何使用setenv()在c ++中导出一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要导出几个变量,使它们在命令行中类似于以下内容。

  export ROS_HOSTNAME = xxx 

如何在c ++中使用setenv()来实现?



谢谢。

解决方案

setenv()手动输入:


概要

  #include< stdlib.h> 
int setenv(const char * envname,const char * envval,int overwrite);



DESCRIPTION

setenv在调用进程的环境中更新或添加变量。 envname参数指向包含要添加或更改的
的环境变量的名称的字符串。环境变量应设置为envval指向的值。如果envname指向包含=字符的字符串,函数将失败。如果
由envname命名的环境变量已经存在,并且overwrite的值不为零,则函数将返回成功并且环境将被更新。如果由envname命名的环境
变量已经存在并且覆盖的值为零,则函数将返回成功,并且环境将保持不变。



如果应用程序修改环境或其指向的指针,setenv()的行为是未定义的。 setenv()函数将更新environ
指向的指针列表。



envname和envval描述的字符串由此函数复制。 p>

setenv()函数不需要重入。不需要可重入的函数不需要线程安全。



返回值

成功完成,返回零。否则,将返回-1,errno设置为指示错误,并且环境不变。


/ p>

  setenv(ROS_HOSTNAME,xxx,1); //覆盖

  setenv(ROS_HOSTNAME,xxx,0); //不会覆盖



>不能使用 setenv()将变量从进程导出到调用进程(shell)!
使用fork创建的子进程将继承当前进程环境定义,因此也将继承您的更改和添加。


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.

解决方案

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.

这篇关于如何使用setenv()在c ++中导出一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆