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. -
相关文档:
今天在写到用c来解析post数据的时候需要用到一个数组变量来放post的所有数据等着来解析,不想太浪费内存了。于是想着先申请一个最大威尔哦content_length大小的数组再说。但是不允许用变量来。比如
int length = atoi(getenv("CONTENT_LENGTH"));
char params[length];
memset(params, '\0', length);
那么char para ......
GCC 支持了许多不同的语言,包括 C、C++、Ada、Fortran、Objective C,Perl、Python 和 Ruby,甚至还有Java。
Linux 内核和许多其他自由软件以及开放源码应用程序都是用 C 语言编写并使用 GCC 编译的。
编译C++程序:
-c 只编译不连接
g++ file1 -c -o file1.o
g++ file2 -c -o file2.o
g++ f ......
可以将XML文件的树(只有一个顶层节点).于是理所当然的可以用树作为XML的一种存储结构.
我将在这里用C++实现对简单的XML文件的解析.
1.选择存储结构:
树型数据结构有多种存储方式,我将用"孩子兄弟表示法",定义如下:
typedef struct CSNode
{
int subNodes;
string data;
string name;
struct CSNode *fi ......