【问题标题】:How can I pass an Integer value from a .cc file to .tcl file, when using ns2?使用 ns2 时,如何将整数值从 .cc 文件传递​​到 .tcl 文件?
【发布时间】:2016-09-15 00:47:01
【问题描述】:

我正在使用 .cc 文件中的变量计算数据包。 现在,我想在我的 .tcl 脚本中获取该值,将其存储在某个变量中并对其执行一些操作? 这怎么可能?

【问题讨论】:

    标签: tcl ns2 cc


    【解决方案1】:

    有几个选项,取决于您如何完成代码和 Tcl 之间的绑定。例如,您可以使用Tcl_NewIntObj() 将您的值转换为可以作为参数传递给Tcl 命令的Tcl 值,或者您可以使用Tcl_LinkVar() 在您的C++ 代码中创建一个变量(应该很长-lived,通常是全局变量)将自身暴露为 Tcl 变量。

    // The interp is the interpreter context, which might come from Tcl_CreateInterp or might
    // be handed to you by callback, depending on what you're doing.
    
    Tcl_LinkVar(interp, "myVarName", (char *) &yourIntVariable, TCL_LINK_INT);
    

    一旦你这样做了,在 Tcl 端读取 myVarName 变量将从你的变量中读取,直到你 Tcl_UnlinkVar() 打破耦合。

    【讨论】:

    • Tcl_NewIntObj 更适合您将值作为参数传递或直接将其写入 Tcl 变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多