moset的个人博客分享 http://blog.sciencenet.cn/u/moset 以心为绢,绘山河;以思为海,汇百流;以文为宴,会高朋。

博文

linux下简单调试C++功能块的方法

已有 4260 次阅读 2013-4-7 14:41 |系统分类:科研笔记| Linux, Python

   时常编写一些C++功能程序,然而测试或者组合它们却是件费力的事。利用python这种被称为“万能胶水”的语言工具可以很好的简化测试过程。下文将简要的列出测试方法的一个范例。

建立动态库所需的头文件:S_fifo.h 内容如下

#include<malloc.h>

template<class T> class  s_fifo

{

public:

    T*  data;

    s_fifo(int k)

    {

        i= k;

        data= (T *)malloc(i*sizeof((T)0));

        start= 0;

        end= i - 1;

        sum= 0;

    }

    ~s_fifo()

    {

        free(data);

    };

    int sum;

    int put(T*);

    int get(T*);

    int put_s(T*, int);

    int get_s(T*, int);

    void clear();

private:

    int start;

    int end;

    int i;        

};

extern"C"{

int  ad(int, int);

}

extern"C"{

void*  creat_s_fifo_int(int);

}

extern"C"{

void  delete_s_fifo_int(void*);

}

extern"C"{

int  s_fifo_put(void*,int*);

}

extern"C"{

int  s_fifo_get(void*,int*);

}

extern"C"{

int  s_fifo_puts(void*,int*,int);

}

extern"C"{

int  s_fifo_gets(void*,int*,int);

}

extern"C"{

void  s_fifo_clear(void*);

}

这是一个管道类,并引出几个接口函数。

它的cpp文件: s_fifo.cpp

G++测试文件:test.cpp

Makefile文件如下:

CC=g++

PROGS=t

p=$(shell pwd)

t:test.cpp s_fifo.so

        $(CC)-o test test.cpp  -ldl

s_fifo.so:s_fifo.cpp s_fifo.h

        $(CC)-fpic -shared -o s_fifo.so s_fifo.cpp s_fifo.h

        echo$@

clean:

        rm-rf *.o

编译生成动态库s_fifo.so,以及连接到测试程序,执行:

[root@localhost fifo]# make

g++ -fpic -shared -o s_fifo.so s_fifo.cpps_fifo.h

echo s_fifo.so

s_fifo.so

g++ -o test test.cpp  -ldl

[root@localhost fifo]# ls

Makefile  s_fifo.cpp   s_fifo.h   s_fifo.so test.cpp   TFIFO.py  test    

[root@localhost fifo]# ./test

ad:77

......0 160539576 re:=10

0 1 2 5 10 17 0 0 0 0 0 0 0 0 0 0[root@localhost fifo]#

每次测试或者组合其它功能模块都要去编写一次main去调用获取动态库是繁琐和低效的。

下面用python来测试:

建立python文件TFIFO.py如下:

#!/usr/bin/python

import os

import re

import ctypes

handle=ctypes.CDLL('./s_fifo.so')

print type(handle)

print'************************************************************'

print handle.ad(12,34)

fifo=handle.creat_s_fifo_int(100)

print type(fifo)

L="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

re = handle.s_fifo_puts(fifo,L,5)

print re

U=16*'^__^'

print U

re = handle.s_fifo_gets(fifo,U,8)

print re

print (U)

re = handle.delete_s_fifo_int(fifo)

保存并执行:

[root@localhost fifo]# ./TFIFO.py

<class 'ctypes.CDLL'>

************************************************************

46

......0<type 'int'>

<type 'list'>

5

^__^^__^^__^^__^^__^^__^^__^^__^^__^^__^^__^^__^^__^^__^^__^^__^

5

1234567890abcdefghij^__^^__^^__^^__^^__^^__^^__^^__^^__^^__^^__^

[root@localhost fifo]#

        其中U是str格式,而我们的类模板生成intfifo类的应用,所以是4char一位,也就是

也就是5×4=20 bytes

在动态库文件接口用extern”C”扩展,python调用。

库的接口函数编写用void *指针转换。

Linux下调用.so添加#include<dlfcn.h> 该头文件需要静态库lib-ldl

Python版本2.5,动态库需加载ctypes模块。

class CDLL(__builtin__.object)

   |  An instance of this classrepresents a loaded dll/shared

   |  library, exporting functionsusing the standard C calling

   |  convention (named 'cdecl' onWindows).

   |  

   |  The exported functions can beaccessed as attributes, or by

   |  indexing with the functionname.  Examples:

   |  

   |  <obj>.qsort ->callable object

   |  <obj>['qsort'] ->callable object

   |  

   |  Calling the functions releasesthe Python GIL during the call and

   |  reaquires it afterwards.

                                                       ……

注:本文只是个人常用的方法,而不是一个标准.









https://wap.sciencenet.cn/blog-567861-677858.html

上一篇:求正弦函数值
下一篇:曾经的语文课上(排序问题)
收藏 IP: 59.46.83.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-30 02:51

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部