Python encapsulation of C + + classes using SIP

Source: Internet
Author: User

There are many tools used in Python in the swig (in English meaning: booze), sip (in English), and Boost.python. SIP is developed from Swig, and is specifically designed for Python to call C + + modules (see SIP naming to see, very interesting).

SIP usage, in the official website of the SIP Reference Guide is introduced, but that is for at least a bit of experience of the personnel, for beginners, want 10 minutes to quickly get started or a little difficult. Here is the introduction of my use of SIP summary of the need to pay attention to the place. (in the convenience of everyone to facilitate their own principle, welcome reprint, without the consent of my own, but please indicate the source, preferably the original connection, figure personal Gas, thank you!) )

As a prerequisite , this machine will have Python, GCC (under Windows can be used with MinGW, in my eclipse configuration CDT in the article about MinGW installation introduction). and add the bin file path under Python.exe and MinGW to the environment variable.

Part I: Installing SIP


To download the SIP zip-format Code package on riverbank and extract it to C:\Python27, the folder path for the SIP is now C: \python27\sip-4.13.2\ sip-4.13.2 (You can also unzip it into any folder, but here for convenience, unzip it into the Python folder on my Computer).

Open the cmd command line for Windows, and the output such as CD C:\Python27\sip-4.13.2\sip-4.13.2 jumps to the SIP folder.

Enter Python win32-g++. Configured to display the following results:

After completing the previous step, enter Mingw32-make, and then enter Mingw32-make install. If no accident, SIP installation is complete.


If you have previously installed other software such as Eclipse on your computer and changed mingw32-make to make, then you will need to change the above Mingw32-make command to make to use.

A friend said he used the above method to use make always error, carefully asked, only to find his computer installed Embacardero rad Development tools, known as Delphi and C + + Builder development tools. After installing this tool, enter make on the command line, using always embacardero C + + Builder's make. Based on the pro-test, even when configured with the Python win32-borland command, the Emcarbadero make is not valid. This configuration command produces the make file, just for the old version of Borland C + + builder's make command.

To use NMAKE for Microsoft VC, use command python win32-msvc in configuration, where win32-msvc is using VC6 nmake using VC2003 nmake;win32-msvc2005 using VC2005 nmake;win32-msvc2008 using the VC2008 nmake. Based on the results shown by the--show-platforms command, the SIP configuration temporarily does not support NMAKE under VC2010 and VS11. If you want to know all the platforms and compilers supported by SIP, you can use the Python command at the command line to view them.


SIP installations like Linux are similar, and even simpler, Linux distributions like Ubuntu automatically install Python and GCC without the need to configure environment variables.

So as long as the official network download SIP Linux under the tar.gz format of the code package, extracted into a directory. Then enter the directory in the terminal, entering the Python linux-g++;make;make install in turn.

A function written in C + + is used in Python.

1, first, write a C file , the function is to add two numbers and output, named ADD.C, this will become the module name in Python, as follows:

/* FILE:ADD.C */int Add (int x, int y) {int g;      g = x + y;  return g; }

2, then, hand-written sip file , in the process of using SIP, a C + + source code file, must correspond to a SIP file of the same name, named Add.sip, as follows:

[Plain] view plaincopy

    1. /* Define the SIP wrapper to the Add library. */

    2. %module (Name=add, language= "C")

    3. int add (int x, int y);

If the source program is written in C + +, then here (Name=add, language= "C") can be omitted.
Here the C source file does not have a header file, so the corresponding SIP file is very simple. If the source code is the implementation part, the implementation section also includes the interface section, which is the header file. In the corresponding SIP file, you need to use the

[CPP] view plaincopy

    1. %typeheadercode

    2. #include <word.h>

    3. %end

To include the corresponding header file. A sip file is similar to a formal C + + header file, but differs from the following: The SIP file does not contain the private member variable (private or protected) of the corresponding header file. For more detailed SIP file authoring specifications, refer to the documentation on the Riverbank official website for SIP Reference Guide.

3, compile C file . According to the official website, is to write, but don't worry, first do some essential work. Compile the add.c into a add.o file on the command line: input

[SQL] view plaincopy

    1. Gcc-c ADD.C

Next, generate the ADD.O file for this step in the production library file:

[Plain] view plaincopy

    1. Ar-r libadd.a ADD.O

These two steps here are for a separate C module test, if it is a large number of C modules, can be used with a makefile batch to complete, which is easy for beginners to blur the place. Remember that you need to copy the Libadd.a file to the Libs folder under the Python folder. you can also compile the source code directly into a DLL with the command

4, hand-written files , the same, the Configure file is not difficult to write, look at the norms will be (to the birds ...) )。 Here, we imitate the official website template to write a own

[python] view plaincopy

  1. Import OS

  2. Import Sipconfig

  3. # The name of the SIP build file generated by SIP and used by the build

  4. # System.

  5. Build_file = "ADD.SBF"

  6. # Get the SIP configuration information.

  7. Config = sipconfig. Configuration ()

  8. # Run SIP to generate the code.

  9. Os.system ("". Join ([Config.sip_bin, "-C", ".", "-B", Build_file, "Add.sip"]))

  10. # Create the Makefile.

  11. Makefile = Sipconfig. Sipmodulemakefile (config, build_file)

  12. # ADD The library we are wrapping. The name doesn ' t include any platform

  13. # Specific prefixes or extensions (e.g. the "Lib" prefix on UNIX, or the

  14. # ". dll" extension on Windows).

  15. Makefile.extra_libs = ["Add"]

  16. # Generate the Makefile itself.

  17. Makefile.generate ()

5, run, will generate a makefile file (directly with idle open, press F5 run, or command line with Python run can).
Here is a strange place, a few friends in this step will be error, said the ADD.SBF file can not be found, and the ADD.SBF file should be runtime call related functions automatically generated. If this problem occurs, please recompile the SIP. If it is under Windows, it is best to copy a complete Python folder with normal sip on another machine and overwrite the problematic Python folder on the problematic machine.

6. Enter make on the command line(this will generate a warning about the function, without it, we are used to test ...) Other should be no problem, if there is a problem, please check the previous steps), generate the Add.pyd file. Then type make install(load the Add.pyd file into the Sit-packages folder in the Python Lib folder).

7. Open the command line for Python and test it:

[python] view plaincopy

    1. >>>import add

    2. >>>add.add (4, 7)

    3. 11

    4. >>>

(Forgive me for this rotten module name ...) )


(1), these files can be placed in the Python folder under the new folder (all operations in this directory in the command-line window to use). Note that Python's parent folder name cannot have spaces, or the library file cannot be read.

(2), using MinGW, you need to add ~\mingw\bin environment variables (Linux is not necessary), so as to use the GCC, make and AR tools.

Python encapsulation of C + + classes using SIP

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: 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.