CentOS Swig compiled C,python call

Source: Internet
Author: User

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(&ltime);returnCTime (&ltime); }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.