Python 使用C代码——swig
Ref : http://www.swig.org/translations/chinese/tutorial.html
假设你有一些c你想再加Python.。举例来说有这么一个文件example.c
/* File : example.c */
#include <time.h>
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
int my_mod(int x, int y) {
return (x%y);
}
char *get_time()
{
time_t ltime;
time(<ime);
return ctime(<ime);
}
接口文件
现在,为了增加这些文件到你喜欢的语言中,你需要写一个接口文件(interface file)投入到swig中。这些C functions的接口文件可能如下所示:
/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
建立Python模块
转换编码C成Python模块很简单,只需要按如下做即可(请见其他操作系统的SWIG 共享库帮助手册):
unix % swig -python example.i
unix % gcc -c -fPIC
-m32
example.c example_wrap.c \
-I/usr/(local/include/python2.1)include/python2.4
unix % (ld)gcc
-shared -m32
example.o example_wrap.o -o _example.so
关于以上红蓝绿颜色字解释:
1. -
相关文档:
本文来自: http://blog.pfan.cn/xiuandfang/24935.html
标签:C C ++ string String 数据结构
C字符串处理函数的实现
C字符串处理函数的实现(Linux)
#include <stddef.h>
char * ___strtok = NULL;
char * strcpy(char * dest,const char *src)
&nb ......
1、 一些头文件的作用:
<assert.h>:ANSI C。提供断言,assert(表达式)
<glib.h>:GCC。GTK,GNOME的基础库,提供很多有用的函数,如有数据结构操作函数。使用glib只需要包含<glib.h>
<dirent.h>:GCC。文件夹操作函数。struct dirent,struct DIR,opendir(),closedir(),readdir(),readdi ......
1、 int a=2,b=11,c=a+b++/a++; 则c值为多少?
【考点】编码规范。
表面上考察你对运算符优先级的掌握程度,但实际上优先级这些玩意很难死记硬背得住?大家的疑惑不就是运算符的结合顺序么?那么如何去避免呢?c=a+((b++)/(a++))不就行了么,其实问题背后考察的是你的编码规范,如何写清晰易懂的代码,如何在一个团 ......
可以将XML文件的树(只有一个顶层节点).于是理所当然的可以用树作为XML的一种存储结构.
我将在这里用C++实现对简单的XML文件的解析.
1.选择存储结构:
树型数据结构有多种存储方式,我将用"孩子兄弟表示法",定义如下:
typedef struct CSNode
{
int subNodes;
string data;
string name;
struct CSNode *fi ......