【问题标题】:tcl file is unable to get environment variable which is set through code [closed]tcl文件无法获取通过代码设置的环境变量[关闭]
【发布时间】:2019-03-25 10:18:49
【问题描述】:

我已经使用 putenv 通过我的代码设置了环境变量。我正在通过 tcl 文件读取环境变量。看来,在 Windows 中,这显示了以下内容 错误:无法读取“env(myvar)”:没有这样的变量, 没有这样的变量 (阅读“env(myvar)”上的跟踪) 从内部调用 “放 $env(myar)”

但在 Linux 中不会出现错误。

putenv 用于设置环境变量 $env() 在 tcl 文件中用于获取环境。

【问题讨论】:

  • “不工作”不是一个有用的问题描述。
  • 问题已编辑,谢谢

标签: c++ linux windows tcl xilinx


【解决方案1】:

这是众所周知的,并且是由于如果我们更精确地同步,从 Tcl 对环境变量的所有访问都会有更大的性能损失,这是一个比你所在的地方更常见的用例从 C 代码更改环境变量。 (putenv() 系统调用没有给我们任何我们可以有效挂钩来检测变化的东西,所以我们最终不得不从头开始重新组装整个env 数组;这被发现是一个重要的瓶颈。)

最简单的解决方法是在 Tcl 中创建一个额外的命令(但用 C 编写),直接包装 getenv() 调用,以便您可以将其与您怀疑可能在 Tcl 背后更改的环境变量一起使用.

int WrapGetEnv(ClientData ignored, Tcl_Interp *interp, int argc, char *argv[]) {
    if (argc != 2) {
        // Probably ought to be a better error here
        return TCL_ERROR;
    }
    char *var = argv[1];
    char *val = getenv(var);
    if (val != NULL) {
        Tcl_SetResult(interp, val, TCL_DYNAMIC);
    }
    return TCL_OK;
}

// ... in the right place do this ...
Tcl_CreateCommand(interp, "getenv", WrapGetEnv, NULL, NULL);

【讨论】:

  • 处理所有错误情况留作练习......
  • 非常感谢您的评论!!。
  • 但这个问题似乎出现在 Windows 和 Linux 中。但就我而言,问题仅出现在 Windows 上。有什么相同的原因吗?
猜你喜欢
  • 2013-04-30
  • 2016-10-02
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
相关资源
最近更新 更多