您查看的文章来源于http://www.oklinux.cn
Tcl 全称为 Tool Command Language.
发音为 Tickle.
Tcl是一种较为独特的脚本语言.它是为了与主应用程序集成而设计的小型语言.
在Linux下安装好了tcl后我们就可以将些相应的脚本来完成一定的功能,至于具体的语法请参考相关的文档.在这里我只是介绍如何在c语言中调用Tcl脚本.
(一)执行相应的命令
C程序代码如下(c_call_tcl_file.c):
#include<stdio.h> #include<stdlib.h> #include<tcl.h> int main(int argc,char**argv) { Tcl_Interp *pTclInterp = Tcl_CreateInterp(); if(!pTclInterp) { printf("Tcl Interpreter could not be created.\n"); return 0; } Tcl_Eval(pTclInterp," puts \"Hello ,world !\""); Tcl_DeleteInterp(pTclInterp); return 0; }
|
编译:
gcc -g -o c_call_tcl_file c_call_tcl_file.c |
出错信息:
/tmp/ccMy2cR7.o(.text+0x11): In function `main': /home/shwq/tcl/c_call_tcl_file.c:6: undefined reference to `Tcl_CreateInterp' /tmp/ccMy2cR7.o(.text+0x3c):/home/shwq/tcl/c_call_tcl_file.c:12: undefined reference to `Tcl_Eval' /tmp/ccMy2cR7.o(.text+0x4a):/home/shwq/tcl/c_call_tcl_file.c:13: undefined reference to `Tcl_DeleteInterp' collect2: ld returned 1 exit status
|
原因: 没有与相关的动态库相连接.
查看库的方法:
显示结果:
libtcl8.3.so (libc6) => /usr/lib/libtcl8.3.so libtcl.so (libc6) => /usr/lib/libtcl.so libswigtcl.so (libc6) => /usr/lib/libswigtcl.so libqassistantclient.so.1.0 (libc6) => /usr/lib/qt-3.1/lib/libqassistantclient.so.1.0 libqassistantclient.so.1 (libc6) => /usr/lib/qt-3.1/lib/libqassistantclient.so.1 libdb_tcl.so (libc6) => /usr/lib/libdb_tcl.so libdb_tcl-4.0.so (libc6) => /usr/lib/libdb_tcl-4.0.so |
重新编译:
gcc -g -o c_call_tcl_file c_call_tcl_file.c -ltcl |
显示编译后结果:
-rwxr-xr-x 1 root root 49003 5月 6 00:59 c_call_tcl_file -rw-r--r-- 1 root root 314 5月 6 00:25 c_call_tcl_file.c |
执行程序:
[root@LoveForLinux tcl]# ./c_call_tcl_file Hello ,world ! |
(二)执行相应的脚本文件
脚本文件如下:hello.tcl
修改相应的C程序文件(c_call_tcl_file.c)
#include<stdio.h> #include<stdlib.h> #include<tcl.h> int main(int argc,char**argv) { Tcl_Interp *pTclInterp = Tcl_CreateInterp(); if(!pTclInterp) { printf("Tcl Interpreter could not be created.\n"); return 0; } if(Tcl_EvalFile(pTclInterp,"hello.tcl") == TCL_ERROR) { printf("excute the tcl shell script error\n"); return 0; }; Tcl_DeleteInterp(pTclInterp); return 0; }
|
编译:
gcc -g -o c_call_tcl_file c_call_tcl_file.c -ltcl |
执行:
输出结果: