Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Using C / C ++ extensions Python     - Easy to install Ubuntu 15.04 and Ubuntu 15.04 GNOME on Wayland trial (Linux)

- MySQL 5.6 use GTIDs build the master database (Database)

- Oracle table of nested loop connection (Database)

- Hadoop2.6.3 build clusters and the development of MapReduce WIN7 by Eclipse on Linux demo (Server)

- Mumble installation source VoIP application on Ubuntu (Linux)

- CentOS / Linux NIC set IP address configuration (Linux)

- Try to use Lets Encrypt (Linux)

- JavaScript basic types and type conversion (Programming)

- RedHat Linux 6 desktop installation (Linux)

- How to deploy Python Web application: Heroku deployment process complete records (Server)

- Hunk / Hadoop: Performance Best Practices (Server)

- Linux-- sub-volume compression and decompression (Linux)

- CentOS 7 Configure logging (VirtualBox) (Linux)

- iptables using summary (Linux)

- Win7 + Ubuntu Kylin + CentOS 6.5 installed three systems (Linux)

- Ubuntu 14.10 / 14.04 / 12.04 virtual users to install the printing software Boomaga (Linux)

- Use mod_wsgi Django application deployment (Server)

- Linux System Getting Started Tutorial: Installing Brother printer in Linux (Linux)

- imp / exp Oracle Database import and export commands (Database)

- Ubuntu 14.04 and derivative version of the user on how to install cURL 7.37.1 (Linux)

  Using C / C ++ extensions Python
  Add Date : 2017-04-13      
  Applications written using the Python script is more convenient, but sometimes need to extend Python for various reasons (such as to provide a Python program interface). Always wanted to organize before the next, finally sat down to do this thing, here recorded, but also so that I can later reference.

Explanation: The test environment the operating system CentOS6.5_x64, python version 2.6

Direct dynamic library calls

1, the preparation of the code module DLL file

Here is an example to the greatest number

Code (callTest1.cpp) as follows:

extern "C"
    int Max (int i1, int i2)
        return (i1> i2) i1:? i2;

Execute the following command in bash:

g ++ -fPIC -shared -o libcallTest1.so callTest1.cpp

Generate dynamic library files libcallTest1.so

2, the dynamic library calls using python

You can call the dynamic library files through ctypes, as follows:

#! / Usr / bin / env python

from ctypes import *
import os

so1 = CDLL (os.getcwd () + '/libcallTest1.so')
print so1.Max (1,3)

Use boost python library extension

This way you can directly generate python module, you can use the import operation to import directly. Of course, using this method, you need to install boost library, the specific operation here is not to say, do not know their friends under investigation.

boost official website: http: //www.boost.org/

1, the module code is as follows:

File name: boostCallTest1.cpp

document content:

int Max (int i1, int i2)
    return (i1> i2) i1:? i2;

#include < boost / python / module.hpp>
#include < boost / python / def.hpp>
using namespace boost :: python;

    def ( "Max", Max);

2, write a module compiled script

File name: setup.py

document content:

#! / Usr / bin / env python

from distutils.core import setup
from distutils.extension import Extension

setup (name = "PackageName",
    ext_modules = [
    Extension ( "boostCallTest1", [ "boostCallTest1.cpp"],
    libraries = [ "boost_python"])

3, compile and test module

Compile the module:

python setup.py build

Will produce boostCallTest1.so file in the build directory, enter the directory can be used as follows:
>>> Import boostCallTest1
>>> BoostCallTest1.Max (1,3)

Extended use python swig

SWIG is a help in C or C ++ to write software capable of embedded development tools coupled with a variety of other high-level programming languages.

SWIG can be used in a variety of different types of languages including common scripting languages such as Perl, PHP, Python, Tcl, Ruby and PHP.

swig official website: http://www.swig.org/download.html

It can be installed directly through yum:
yum install swig

1, write code

File name: swigCall.cpp

It reads as follows:

int Max (int i1, int i2)
    return (i1> i2) i1:? i2;

2, write interfaces file

File name: swigCall.i

It reads as follows:
% Module swigCall
% {
extern int Max (int i1, int i2);

extern int Max (int i1, int i2);

3, the preparation of the Makefile

It reads as follows:
    make swigCall

    swig -python -c ++ swigCall.i
    g ++ -c swigCall.cpp swigCall_wrap.cxx -I / usr / include / python2.6 / -fPIC
    g ++ -shared swigCall.o swigCall_wrap.o -o _swigCall.so

    rm -f swigCall_wrap.c *
    rm -f * .py *
    rm -f * .so
    rm -f * .o

Note: swig command parameters to be used -c ++ compiler c ++ Code

Use SIP extensions python

SIP is developed from SWIG, calling specifically for the Python C / C ++ module. Note that the SIP and the voip sip not the same thing, here is an extension of python used sip, sip voip in a communication protocol, do not confuse.


You need to install python sip library;

1, write c ++ module

1.1 header files prepared

File name: sipCall.h contents of the file:

class TestMode
    int Max (int i1, int i2);

1.2 write module content

File name: sipCall.cpp contents of the file:

#include "sipCall.h"

int TestMode :: Max (int i1, int i2)
    return (i1> i2) i1:? i2;

2, write interfaces file

File name: sipCall.sip contents of the file:
% Module TestMode

class TestMode {

% TypeHeaderCode
#include "sipCall.h"
% End

    int Max (int i1, int i2);

3, static library survival

Here with scripts, file name: genlib.sh

document content:
#! / Bin / bash
g ++ -c -fPIC sipCall.cpp
ar -crs libsipCall.a sipCall.o

4, the preparation configure file

The script is used to generate Makefile, which reads as follows:

#! / Usr / bin / env python

import os
import sipconfig

build_file = "sipCall.sbf"
config = sipconfig.Configuration ()
cmd = "" .join ([config.sip_bin, "-c", ".", "-b", build_file, "sipCall.sip"])
os.system (cmd)

makefile = sipconfig.SIPModuleMakefile (config, build_file)
makefile.extra_libs = [ "sipCall"]
makefile.LIBDIR.append ( ".")
makefile.generate ()

5, run the script to generate genlib.sh lib file;

6, run configure.py script generation Makefile;

7, run the make file generation module (so the file);

8, python tests are as follows:
>>> Import TestMode
>>> Dir (TestMode)
[ 'TestMode', '__doc__', '__file__', '__name__', '__package__']
>>> S = TestMode.TestMode ()
>>> S.Max (1,3)
- struts2 completely the wrong way to capture 404 (Programming)
- Practical top command (Linux)
- Install Firefox 28 on Ubuntu, Linux Mint (Linux)
- CentOS card fails to start at boot progress bar certmonger solve (Linux)
- Linux operating system boot manager -GRUB (Linux)
- The Sublime Text 3 configuration file (Linux)
- Ubuntu How to install screen recording tool Simple Screen Recorder 0.3.1 (Linux)
- Ubuntu 14.04 next upgrade to GNOME 3.12 (Linux)
- Linux Apache server security (Linux)
- Linux remote connectivity tools -OpenSSH (Linux)
- Oracle 10g, 11g database silent installation of small differences (Database)
- Linux monitoring tools introduced series --smem (Server)
- The difference between Objective-C language nil, Nil, NULL, NSNull (Programming)
- Linux Oracle environment variable is invalid Problem Solving (Database)
- Swift used in the application to add a local push at the specified time (Programming)
- Python-- for anomalies and reflection of objects articles (Programming)
- Linux SSH commands (Linux)
- Expert advice: Do not use the computer security IE browser (Linux)
- 5 tips to improve your Linux desktop security (Linux)
- STL source code analysis - iterator each container classification (Programming)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.