TCL:在 C 中嵌入时调用 TCL 包

TCL: call TCL package while embedded in C

this question我了解到我们可以像下面的方法一样简单地将TCL嵌入C中

#include <stdio.h>
#include <tcl.h>
void main ()
{
   Tcl_Interp *myinterp;
   char *action = "set a [expr 5 * 8]; puts $a";
   int status;
   printf ("Your Program will run ... \n");
   myinterp = Tcl_CreateInterp();
   status = Tcl_Eval(myinterp,action);
   printf ("Your Program has completed\n");
   getch();
}

要编译它,我们需要定义 tcl 库的路径:

 gcc -o test.exe test.c -Ic:/tcl/include /mingw64/bin/tcl86.dll

我的问题是:如果我的 tcl 脚本正在调用另一个包(例如:package require Img),如何在创建的 [=25] 中包含这个包(例如“Img”) =].

我在 windows 上使用 mingw64 来编译我的 C 代码,但是当 运行 编译结果 test.exe 时,它给了我 TCL 错误 {can't find package Img在执行“package require Img”时}

顺便说一句,我安装了 Img,当我 运行 我的 TCL 脚本使用 tclsh 时,我没有错误。

您应该使用指向您希望能够访问的额外库的位置(即目录)的路径扩展全局 auto_path 变量中的列表。

Tcl_SetVar(interp, "::auto_path", "/path/to/directory", TCL_APPEND_VALUE | TCL_LIST_ELEMENT);

在创建解释器之后但在评估其中的任何脚本之前执行此操作。这对于路径名中的空格等字符是安全的。在 Windows 上,如果您愿意,可以使用 \ 作为分隔符。如果您有多个位置,请多次调用 Tcl_SetVar()。(如何计算出正确的目录由您决定;该值会立即被复制。)