Using SIP to encapsulate C + + as Python

Source: Internet
Author: User

SIP is a tool for generating C + + interface code for Python, similar to SWIG, but using a different interface format. The idea originated in Swig, primarily to create the QT package for Python, which was used to create PyQt and Pykde , and to support the QT Signal/slot system.


This article mainly introduces the compilation and installation of SIP and the birth of C + + code into Python under the window platform. This article is on the premise of the installation of Python, Python installation package, can be downloaded on its official website, here no longer cumbersome!


1.SIP Compilation and Installation

On SIP website http://www.riverbankcomputing.com/software/sip/download download SIP, the latest version is 4.16.7, I downloaded the latest version. Unpack the SIP, Open the Windows cmd command line, enter the SIP root directory, enter the python configure.py--show-platform to view the supported platforms. 4.16.7 supports the following platforms:


As can be seen from the above, SIP currently does not support VS2012, but opens the SIP root directory specs to view the supported platforms and discovers that it is possible to support VS2012 by modifying its platform configuration file.

Copy the copy of the win32-msvc2010, modify it to win32-msvc2012, open the win32-msvc2012 with a text editor, and modify the contents:

will be Qmake_compiler_defines + = _msc_ver=1600 WIN32 modified to Qmake_compiler_defines + = _msc_ver=1700 WIN32

will qmake_cflags =-nologo-zm200-zc:wchar_t -Modified to qmake_cflags =-nologo-zm200-zc:wchar_t

Go back to the SIP root and open configure.py with a text editor to replace the contents of the red callout below with win32-msvc2012:


SIP Support VS2012 Now!

Open the VS2012 developer command prompt, go to the SIP root and execute the following command in turn:

python configure.py--platform win32-msvc2012

Nmake

NMAKE Install

The first statement mainly generates the makefile file


2.c++->python

1) using VS to create a static library of the project, the main implementation of string reverse, the content is as follows:

. h file

#ifndef pysiptest_include_h#define pysiptest_include_h#include <string>using namespace Std;class Word {private: Const string The_word;public:word (const char *w): The_word (w) {};const char* Reverse () const;}; #endif
CPP File

#include <string> #include <algorithm> #include "PySIPTest.h" const char* word::reverse () const{string strtemp = The_word;std::reverse (Strtemp.begin (), Strtemp.end ()); return Strtemp.c_str ();}


2) write configure.py files and *.sip files

<span style= "FONT-SIZE:18PX;" >//Word.sip%module wordclass Word {%typeheadercode#include <string> #include <word.h>%end public:word (    Const char* W); Const CHAR* reverse () const;}; # Configure.pyimport OS, sipconfig, sys# the name of the SIP build file generated by SIP and used by the build System.buil D_file = "WORD.SBF" # Get the SIP configuration information.# documentation:http://pyqt.sourceforge.net/docs/sip4/build _system.htmlconfig = Sipconfig. Configuration () # Run SIP to generate the Code.cmd = "". Join ([Config.sip_bin, "-C", ".", "-B", Build_file, "Word.sip"]) pri NT ("running command" + cmd) os.system (cmd) # Create The Makefile.makefile = Sipconfig. Sipmodulemakefile (config, build_file) # ADD The library we're wrapping.# the name doesn ' t include any platform specific PR Efixes or extensions.makefile.extra_libs = ["word"]# Generate the Makefile itself.makefile.generate () </span>

Run configure.py , a Makefile file is generated (open configure.py directly with idle, press F5 to run, or command line can be run with Python configure.py)

Perform the NMAKE command build, NMAKE install.

In this way, C + + is encapsulated as Python, and if no directory is set, the default Python directory \lib\site-packages is installed.


3. Running

Open the command line for Python and test it:

The Python 2.X Series test code is as follows:

<span style= "FONT-SIZE:18PX;" >import Wordword. Word ("Siptest"). Reverse () </span>

The Python 3.x series test code is as follows:

<span style= "FONT-SIZE:18PX;" >import Wordword. Word (b "siptest"). Reverse () </span>

SIP is very powerful, this article is only superficial introduction of a sip of something, you are welcome to correct.


At the same time Welcome to reprint, reprint please indicate the source!









Using SIP to encapsulate C + + as Python

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.