为Tcl编写C的扩展库

摘要:
=2){//checkargs,sameasmainfunctionargs.Tcl_SetResult;returnTCL_ERROR;}printf;//returnstring。Tcl_设置结果;returnTCL_OK;}intMyimpltcl_Initt{//初始化操作.Tcl_CreateCommand;Tcl_CreateCommand;returnTCL_OK;}intMyimpltcl_卸载{//destroyoperation.returnTCL_OK;}分析:tcl H是加载tcl所需的头文件。初始化函数MyimpltclInit使用Tcl_ CreateCommand函数创建一个可以在Tcl脚本中调用的函数。函数的实现指向C实现的函数。可以在创建方法Tcl_ CreateCommandAction_ FuncAintAction_ FuncTcl_ Create CommandAction_ FuncBintAction_ FuncB Exit function Myimpltcl_ Unload Tcl中调用的函数的名称是卸载动态库时将调用的函数。它用于检查内存和其他资源是否可用。Tcl加载用C编写的so库的规则是。

Tcl是一个比较简洁的脚本语言,官方地址 http://www.tcl.tk.

tcl脚本加载C实现的动态库非常方便。

1. 为Tcl编写一个用C实现的扩展函数。

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

extern "C" {
    // extern for C++.
    int Myimpltcl_Init(Tcl_Interp *Interp);
    int Myimpltcl_Unload(Tcl_Interp *Interp);
}

int Action_FuncA(int notUsed, Tcl_Interp *interp, int argc, char **argv) {
    if (argc != 3) {
        // check args, same as main function args.
        Tcl_SetResult(interp, "Usage::Action_FuncA arg1 arg2",
            TCL_VOLATILE);
        return TCL_ERROR;
    }
    printf("argv[1] is %s.
", argv[1]);
    printf("argv[2] is %s.
", argv[2]);
    // return string.
    Tcl_SetResult(interp, "return of Action_FuncA", TCL_VOLATILE);
    return TCL_OK;
}

int Action_FuncB(int notUsed, Tcl_Interp *interp, int argc, char **argv) {
    if (argc != 2) {
        // check args, same as main function args.
        Tcl_SetResult(interp, "Usage::Action_FuncB arg1",
            TCL_VOLATILE);
        return TCL_ERROR;
    }
    printf("argv[1] is %s.
", argv[1]);
    // return string.
    Tcl_SetResult(interp, "return of Action_FuncB", TCL_VOLATILE);
    return TCL_OK;
}

int Myimpltcl_Init(Tcl_Interp *Interp) {
    // initialize operation.
    Tcl_CreateCommand (Interp, "Action_FuncA", (Tcl_CmdProc *)Action_FuncA, 0, 0);
    Tcl_CreateCommand (Interp, "Action_FuncB", (Tcl_CmdProc *)Action_FuncB, 0, 0);
    return TCL_OK;
}

int Myimpltcl_Unload(Tcl_Interp *Interp, int flags) {
    // destroy operation.
    return TCL_OK;
}

分析:

tcl.h是加载tcl需要头文件。

初始化函数 Myimpltcl_Init

  使用Tcl_CreateCommand函数创建一个可以在tcl脚本中调用的函数,函数的实现指向C实现的函数。

创建方法Tcl中可以调用的函数名称C中实现的函数名称
Tcl_CreateCommand 
Action_FuncA
int Action_FuncA(int notUsed, Tcl_Interp *interp, int argc, char **argv)
Tcl_CreateCommand 
Action_FuncB
int Action_FuncB(int notUsed, Tcl_Interp *interp, int argc, char **argv)


退出函数 Myimpltcl_Unload

  tcl卸载动态库时会调用的函数,用于是否内存和其他的资源。

2. 编写Makefile文件

CC = gcc -g -O3 -w
SHARED_FLAG = -fPIC -shared
PROJECT = libmyimpltcl.so

INC  = -I./
INC += -I$(TCL_HOME)/include
LIB = -L$(TCL_HOME)/lib -ltcl8.5

all : $(PROJECT)

$(PROJECT) :
	$(CC) myimpltcl.cpp ${SHARED_FLAG} -o $(PROJECT) $(INC) $(LIB)

clean:
	rm -rf *.o *.a *.so

分析:

生成的动态库名称必须是libmyimpltcl.so,为什么呢?

Tcl加载C编写的so库的规则是。

void *handle = dlopen("libmyimpltcl.so", RTLD_NOW | RTLD_GLOBAL);

将so库的名称去掉lib前缀

 libmyimpltcl.so 

把去掉前缀的第一个字母变成大写并增加后缀_Init

myimpltcl --> Myimpltcl_Init

拼接成新的字符串作用动态库的入库函数,用dlsym系统调用得到so中的C函数地址,并执行

dlsym(handle, "Myimpltcl_Init");

3. 测试

[user@host tcl]# tclsh
% load libmyimpltcl.so
% # 加载编译好的so库
% info loaded
% # 查看加载过的库信息
{libmyimpltcl.so Myimpltcl}
% set ret [Action_FuncA param1 param2]
% # 调用so中的C函数Action_FuncA
argv[1] is param1.
argv[2] is param2.
return of Action_FuncA
% puts $ret
return of Action_FuncA
% set retB [Action_FuncB 123]
% # 调用so中的C函数Action_FuncB
argv[1] is 123.
return of Action_FuncB
% puts $retB
return of Action_FuncB

  

Done.

免责声明:文章转载自《为Tcl编写C的扩展库》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇编辑DataTable的方法iOS开发——实时监控网速(仅作参考,发现一点问题)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[原创].在Quartus II中分配管脚的两种常用方法

示范程序 seg7_test.v /* * seg7 x 8 查找表测试文件 */ module seg7_test( input CLOCK_50, output [7:0] SEG7_DIG, output [7:0] SEG7_SEG ); seg7_8_LUT u0( .i_clock(CLOCK_...

Linux Tcl和Expect的安装

一、先安装Tcl 1、下载:tcl版本 8.4.19 http://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz/download 2、解压缩源码包 tar xfvz tcl8.4.19-src.tar.gz 3、安装配置 #cd tcl8.4.19/unix #....

原!linux机器 配置自动scp脚本

 方式一: 1.安装相关依赖包 yum install -y tcl tclx tcl-develyum -y install expect 2.脚本 scp.sh #!/usr/bin/expect #获取输入参数set f1 [lindex $argv 0]set f2 [lindex $argv 1]set dir [lindex $argv 2]s...

[NS2]TCL语言基本语法

(来自:《NS2仿真实验-多媒体和无线网络通信》) 1. 变量(Variable)和变量替换(Variable Substitution)   tcl变量是在第一次使用set的指令来指派变量的值时所产生的。可以使用unset来取消这个变量。当取用或者改变变量值时,只要在变量名称前加上一个“$”,就可以取用或改变变量内的值。指令eval用于执行一个tcl s...