如何在c代码中使用tcl API

如何在c代码中使用tcl API,tcl,Tcl,我想在另一个“c”代码文件中使用我的tcl代码的一些功能(API)。但我不知道如何做到这一点,特别是如何链接它们。为此,我采用了一个非常简单的tcl代码,其中包含一个API,该API将两个数字相加并打印总和。有人能告诉我如何调用这个tcl代码来获得总和吗。如何编写一个c包装器来调用这个tcl代码。下面是我正在使用的示例tcl程序: #!/usr/bin/env tclsh8.5 proc add_two_nos { } { set a 10 set b 20 set c [expr

我想在另一个“c”代码文件中使用我的tcl代码的一些功能(API)。但我不知道如何做到这一点,特别是如何链接它们。为此,我采用了一个非常简单的tcl代码,其中包含一个API,该API将两个数字相加并打印总和。有人能告诉我如何调用这个tcl代码来获得总和吗。如何编写一个c包装器来调用这个tcl代码。下面是我正在使用的示例tcl程序:

#!/usr/bin/env tclsh8.5
proc add_two_nos { } {

set a 10

  set b 20

  set c [expr { $a + $b } ]

  puts " c is $c ......."

}

要从C代码计算脚本,请使用或其近亲之一。为了使用该API,您需要在Tcl库中进行链接,并保存执行上下文。另外,您确实应该做一些工作来检索结果并将其打印出来(将脚本错误打印出来尤其重要,因为这对调试有很大帮助!)

因此,您可以得到如下结果:

#包括
#包括
#包括
int main(int argc,字符**argv){
Tcl_Interp*Interp;
int代码;
字符*结果;
Tcl_FindExecutable(argv[0]);
interp=Tcl_CreateInterp();
code=Tcl_Eval(interp,“source myscript.Tcl;添加两个编号”);
/*检索结果*/
结果=Tcl_GetString(Tcl_GetObjResult(interp));
/*检查是否有错误!如果有错误,则返回消息*/
如果(代码==TCL\U错误){
fprintf(stderr,“脚本中的错误:%s\n”,结果);
出口(1);
}
/*如果非空,则打印(正常)结果;我们现在将跳过处理编码*/
如果(strlen(结果)){
printf(“%s\n”,结果);
}
/*清理*/
Tcl_DeleteInterp(interp);
出口(0);
}

要从C代码计算脚本,请使用或其近亲之一。为了使用该API,您需要在Tcl库中进行链接,并保存执行上下文。另外,您确实应该做一些工作来检索结果并将其打印出来(将脚本错误打印出来尤其重要,因为这对调试有很大帮助!)

因此,您可以得到如下结果:

#包括
#包括
#包括
int main(int argc,字符**argv){
Tcl_Interp*Interp;
int代码;
字符*结果;
Tcl_FindExecutable(argv[0]);
interp=Tcl_CreateInterp();
code=Tcl_Eval(interp,“source myscript.Tcl;添加两个编号”);
/*检索结果*/
结果=Tcl_GetString(Tcl_GetObjResult(interp));
/*检查是否有错误!如果有错误,则返回消息*/
如果(代码==TCL\U错误){
fprintf(stderr,“脚本中的错误:%s\n”,结果);
出口(1);
}
/*如果非空,则打印(正常)结果;我们现在将跳过处理编码*/
如果(strlen(结果)){
printf(“%s\n”,结果);
}
/*清理*/
Tcl_DeleteInterp(interp);
出口(0);
}

我想我已经解决了。你是对的。问题在于我使用的include方法。我在c代码中包含了tcl.h、tclDecls.h和tclPlatDecls.h文件,但这些文件不存在于/usr/include路径中,因此我将这些文件复制到该目录中,这可能是不正确的做法。最后,我没有将这些文件复制到/usr/include,并在编译时给出了include路径。我已经创建了可执行文件,它在终端上给出了正确的结果。谢谢你的帮助

下面是我使用的c代码:

#include <tcl.h>
#include <tclDecls.h>
#include <tclPlatDecls.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main (int argc, char **argv) {
Tcl_Interp *interp;
int code;
char *result;

printf("inside main function \n");
//    Tcl_InitStubs(interp, "8.5", 0);
Tcl_FindExecutable(argv[0]);
interp = Tcl_CreateInterp();
code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");

/* Retrieve the result... */
result = Tcl_GetString(Tcl_GetObjResult(interp));

/* Check for error! If an error, message is result. */
if (code == TCL_ERROR) {
    fprintf(stderr, "ERROR in script: %s\n", result);
    exit(1);
}

/* Print (normal) result if non-empty; we'll skip handling encodings for now */
if (strlen(result)) {
    printf("%s\n", result);
}

/* Clean up */
Tcl_DeleteInterp(interp);
exit(0);
我已经执行了文件simple_addition_op,得到了以下正确的结果

inside main function 
c is 30 .......

我特别感谢多纳尔·费罗斯和约翰

我想我已经解决了这个问题。你是对的。问题在于我使用的include方法。我在c代码中包含了tcl.h、tclDecls.h和tclPlatDecls.h文件,但这些文件不存在于/usr/include路径中,因此我将这些文件复制到该目录中,这可能是不正确的做法。最后,我没有将这些文件复制到/usr/include,并在编译时给出了include路径。我已经创建了可执行文件,它在终端上给出了正确的结果。谢谢你的帮助

下面是我使用的c代码:

#include <tcl.h>
#include <tclDecls.h>
#include <tclPlatDecls.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main (int argc, char **argv) {
Tcl_Interp *interp;
int code;
char *result;

printf("inside main function \n");
//    Tcl_InitStubs(interp, "8.5", 0);
Tcl_FindExecutable(argv[0]);
interp = Tcl_CreateInterp();
code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");

/* Retrieve the result... */
result = Tcl_GetString(Tcl_GetObjResult(interp));

/* Check for error! If an error, message is result. */
if (code == TCL_ERROR) {
    fprintf(stderr, "ERROR in script: %s\n", result);
    exit(1);
}

/* Print (normal) result if non-empty; we'll skip handling encodings for now */
if (strlen(result)) {
    printf("%s\n", result);
}

/* Clean up */
Tcl_DeleteInterp(interp);
exit(0);
我已经执行了文件simple_addition_op,得到了以下正确的结果

inside main function 
c is 30 .......

我特别感谢多纳尔·费罗斯和约翰

对此表示抱歉。我现在已经接受了对我很有帮助的几个答复。有谁能回答这个问题吗。为此,mesorry非常紧迫。我现在已经接受了对我很有帮助的几个答复。有谁能回答这个问题吗。我有急事。首先谢谢你的答复。我会尽快尝试并回复你。再次感谢您,我在尝试此操作时遇到以下错误:simple_addition_wrapper_new.c:(.text+0x12):对
Tcl_FindExecutable'simple_addition_wrapper_new.c:(.text+0x17):对
Tcl_CreateInterp'simple_addition_wrapper_new.c:(.text+0x2f)的未定义引用:未定义引用
Tcl\u Eval'simple\u addition\u wrapper\u new.c:(.text+0x3f):未定义引用
Tcl\u GetObjResult'simple\u addition\u wrapper\u new.c:(.text+0x47):未定义引用
Tcl\u GetString'simple\u addition\u wrapper\u new.c:(.text+0x9f):未定义引用
Tcl\u deleteeinterp'请就此回复。我还不能解决这个问题。我尝试过使用不同版本的tcl,但仍然存在上述错误。请回复这些问题,我仍在努力解决这个问题。可能tcl。h不在您的包含路径上。首先感谢您的回复。我会尽快尝试并回复你。再次感谢您,我在尝试此操作时遇到以下错误:simple_addition_wrapper_new.c:(.text+0x12):对
Tcl_FindExecutable'simple_addition_wrapper_new.c:(.text+0x17):对
Tcl_CreateInterp'simple_addition_wrapper_new.c:(.text+0x2f)的未定义引用:对
Tcl\u Eval'simple\u addition\u wrapper\u new.c:(.text+0x3f):对
Tcl\u GetObjResult'simple\u addition\u wrapper\u new.c:(.text+0x47):对
Tcl\u GetString'simple\u addition\u wrapper\u new.c:(.text+0x9f)的未定义引用: