The text of this text connection is: http://blog.csdn.net/freewebsys/article/details/47259413 not allowed to reprint without the Bo master.
1, about Swig
Swig is a development tool that helps to embed joins with other high-level programming languages with software written in C or C + +. Swig can be applied to a variety of different types of languages including common scripting languages such as Perl, PHP, Python, TCL, Ruby and PHP.
The latest version has started to support Golang.
2, official example
First installs the Swig, the official website newest is 3.0.6 version.
You can use the Yum install Swig (this installation is version 2.0.10)
Download the source code to install the latest directly.
Http://www.swig.org/download.html
tar -zxvf swig-3.0.6.tar.gzcd swig-3.0.6install#python 需要安装开发包install python-devel
Http://www.swig.org/translations/chinese/tutorial.html
First create the EXAMPLE.I interface file:
/ * EXAMPLE.I * /%ModuleExample%{/ * Put header files here or function declarations like below * / extern Doublemy_variable;extern intFactintn);extern intMy_mod (intXinty);extern Char*get_time (); %}extern Doublemy_variable;extern intFactintn);extern intMy_mod (intXinty);extern Char*get_time ();
Write the implementation file for the interface example.c:
/* FILE:EXAMPLE.C*/ #include <time.h>Double my_variable =3.0;intFactintN) {if(N <=1)return 1;Else returnN*fact(n1); }intMy_mod (int x,int y) {return(x%y); } Char*get_time() {time_t ltime; Time(<ime);returnCTime (<ime); }
To compile a Python module:
#编译python模块,会生成一个swig -python example.i 编译c文件,和wrap文件,使用python库编译gcc -c -fPIC example.c example_wrap.c -I/usr/include/python2.7#生成共享库ld -shared example.o example_wrap.o -o _example.so
To test the Python module:
>>> import example>>> example.fact(5)120>>> example.my_mod(10,30)10>>> example.get_time()‘Mon Aug 3 05:25:35 2015\n‘>>> example.get_time()‘Mon Aug 3 05:25:37 2015\n‘>>> example.get_time()‘Mon Aug 3 05:25:38 2015\n‘
3, Summary
The text of this text connection is: http://blog.csdn.net/freewebsys/article/details/47259413 not allowed to reprint without the Bo master.
Swig is very handy for implementing the Python module extension, which encapsulates the existing C,c++ library as a module for Python calls.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
CentOS Swig compiled C,python call