【问题标题】:getenv() returns different values in different programs for the same environment variablegetenv() 在不同程序中为同一个环境变量返回不同的值
【发布时间】:2017-10-26 11:26:15
【问题描述】:

我使用的是 ubuntu 14.04。我在程序的 main() 顶部添加了以下代码,但运行时没有打印任何内容。

char const* masteruri = getenv("ROS_MASTER_URI");
if (masteruri!=NULL){
printf ("The current masteruri is: %s",masteruri);
}
char const* rosdis = getenv("ROS_DISTRO");
if (rosdis!=NULL){
printf ("\n The current rosdis is: %s",rosdis);
}

但另一个程序中的相同代码给出了预期的结果

 The current masteruri is: http://localhost:11311
 The current rosdis is: indigo

我已经包含了 stdlib.h 并且我在 etc/profile.d 中导出了变量

【问题讨论】:

  • 在一个程序中在另一个程序中 什么程序?
  • 还有,这些程序怎么运行,上下文很重要。

标签: c++ ubuntu-14.04 ros getenv


【解决方案1】:

我不知道为什么,但是 printf 由于某种原因无法正常工作,我尝试了 DEVLOG_INFO 并注意到在 /etc/profile.d 中导出的变量值可以由两个程序使用 getenv() 返回。

【讨论】:

    猜你喜欢
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2014-03-17
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多