c语言使用tcl单元测试,C语言转成TCL语言

C是应用最广泛的语言,拥有最庞大的用户。

TCL特别是脚本语言是最适合自动化的语言。

如果有一种工具,可以把C或者C++的代码转成TCL或者其他脚本语言的代码,岂不是很爽,今天就介绍这样一个工具SWIG。

SWIG是什么?

SWIG是一个可以把C/C++语言自动转变成TCL/JAVA/Perl/PHP/Python等语言的动态链接库的一个工具。

当前支持Windows/LINUX/Solaris操作系统

以下的所有Demo都是在LINUX上做的试验

如何安装SWIG?

从SWIG的官方网站下载 http://www.swig.org/

把安装包放到LINUX机器的 / 根目录下,用root用户解包, 然后按照下列步骤进行安装:

–cd swig-1.3.12/SWIG-1.3.12

–chmod –Rf 755 *

–./configure

–make

–make install

工具swig命令应该被装在了/usr/local/bin目录下, 安装完成!

如何把一个C函数库转变成TCL动态链接库?

假设有一个.C文件名字为:Hello.c

此c文件包含下列几个函数:

–void PrtMsg(char * msg);

–int Add(int a, int b);

–int Sub(int a, int b);

文件内容如下:

++++++++++++++++++++++++++++++++++++++++++

void PrtMsg(char *msg)

{

printf(“The message is : %s\n”, msg);

}

int Add(int a, int b)

{

return a+b;

}

int Sub(int a, int b)

{

return a-b;

}

+++++++++++++++++++++++++++++++

生成Hello.i文件, 内容如下:

++++++++++++++++++++++

%module Hello

extern void PrtMsg(char *msg);

extern int Add(int a, int b);

extern int Sub(int a, int b);

++++++++++++++++++++++++++++++++++++++

运行如下命令:

–swig –tcl Hello.i

此命令会自动生成一个文件名为:Hello_wrap.c

–gcc –c Hello.c

–gcc –c Hello_wrap.c

–gcc –shared Hello.o Hello_wrap.o –o Hello.so

创建一个TCL脚本文件(Hello.tcl), 内容如下:

++++++++++++++++++++++++++++++++++++++

Load ./Hello.so

PrtMsg “This is a testing”

Set a [Add 10 120]

Set b [Sub 10 120]

Puts “The a value is: $a”

Puts “The b value is: $b”

++++++++++++++++++++++++++++++++++++++

然后在命令行提示符下键入 tclsh Hello.tcl, 执行结果如下:

This is a testing

The a value is: 130

The b value is: -110

如何把一个C++函数库转变成TCL动态链接库

创建3个文件, 内容如下:

–Hello.h

++++++++++++++++++++++++++++++++++++++++++++

class Shape {

public:

Shape() { nshapes++; }

virtual ~Shape() { nshapes–; };

double x, y;

void move(double dx, double dy);

virtual double area(void) = 0;

virtual double perimeter(void) = 0;

static int nshapes;

};

class Circle : public Shape {

private:

double radius;

public:

Circle(double r) : radius(r) { };

virtual double area(void);

virtual double perimeter(void);

};

class Square : public Shape {

private:

double width;

public:

Square(double w) : width(w) { };

virtual double area(void);

virtual double perimeter(void);

};

+++++++++++++++++++++++++++++++++++++++++++++++++

Hello.cxx文件内容如下:

++++++++++++++++++++++++++++++++++++++++++

#include “example.h”

#define M_PI 3.14159265358979323846

void Shape::move(double dx, double dy) {

x += dx;

y += dy;

}

int Shape::nshapes = 0;

double Circle::area(void) {

return M_PI*radius*radius;

}

double Circle::perimeter(void) {

return 2*M_PI*radius;

}

double Square::area(void) {

return width*width;

}

double Square::perimeter(void) {

return 4*width;

}

+++++++++++++++++++++++++++++++++++++++++++

Hello.i文件内容:

+++++++++++++++++++++++++++++++++++++++++

%module Hello

%{

#include “Hello.h”

%}

%include “Hello.h“

+++++++++++++++++++++++++++++++++++++++++

然后执行如下命令:

–swig –c++ -tcl Hello.i

–g++ -c Hello.cxx

–g++ -c Hello_wrap.cxx –I/usr/local/include

–g++ -shared Hello.o Hello_wrap.o –o Hello.so

现在生成一个测试的tcl文件:

+++++++++++++++++++++++++++++++++++++++++++++

load ./Hello.so

puts “Creating some objects:”

Circle c 10

puts ” Created circle [c cget -this]”

Square s 10

puts ” Created square [s cget -this]”

puts “\nA total of $Shape_nshapes shapes were created”

# Set the location of the object

c configure -x 20 -y 30

s configure -x -10 -y 5

puts “\nHere is their current position:”

puts ” Circle = ([c cget -x], [c cget -y])”

puts ” Square = ([s cget -x], [s cget -y])”

# —– Call some methods —–

foreach o “c s” {

puts ” [$o cget -this]”

puts ” area = [$o area]”

puts ” perimeter = [$o perimeter]”

}

# Note: this invokes the virtual destructor

rename c “”

rename s “”

puts “$Shape_nshapes shapes remain”

puts “Goodbye“

+++++++++++++++++++++++++++++++++++++++++++++++

即可完成。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值