The introduction of python 3 tool library boost using c + + to write extensions for python

Keywords: Linux Python Anaconda sudo

Summary

There are different ways to extend Python with C + +:

  • Swig
  • With Boost.Python, you can choose to use Py + + preprocessing
  • Use python.

Before the advent of python, Boost.Python was the best way to write C + + extension modules.

Boost.Python is integrated in Boost C++ Libraries. To install on Ubuntu system;

$ sudo apt-get install libboost-python-dev 
$ sudo apt-get install python-dev

quick get start

hellomodule.cpp

#include <iostream>

using namespace std;

void say_hello(const char* name) {
    cout << "Hello " <<  name << "!\n";
}

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

BOOST_PYTHON_MODULE(hello)
{
    def("say_hello", say_hello);
}

setup.py

#!/usr/bin/env python
# Technical support qq group: 144081101 591302926 567351477 nail free group: 21745728

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

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

Compile:

 python2.7 setup.py build
running build
running build_ext
building 'hello' extension
creating build/temp.linux-x86_64-2.7
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-nbjU53/python2.7-2.7.15~rc1=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c hellomodule.cpp -o build/temp.linux-x86_64-2.7/hellomodule.o
cc1plus: warning: command line option '-Wstrict-prototypes' is valid for C/ObjC but not for C++
creating build/lib.linux-x86_64-2.7
x86_64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-nbjU53/python2.7-2.7.15~rc1=. -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-nbjU53/python2.7-2.7.15~rc1=. -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/hellomodule.o -lboost_python -o build/lib.linux-x86_64-2.7/hello.so

Implementation:

$ python2.7
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> hello.say_hello("World")
Hello World!

What about Python 3?

$ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.65.1: undefined symbol: PyClass_Type

setup.py

#!/usr/bin/env python

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

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

Compile:

$ python3 setup.py build
running build
running build_ext
building 'hello' extension
creating build
creating build/temp.linux-x86_64-3.6
gcc -pthread -B /usr/local/anaconda/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/anaconda/include/python3.6m -c hellomodule.cpp -o build/temp.linux-x86_64-3.6/hellomodule.o
cc1plus: warning: command line option '-Wstrict-prototypes' is valid for C/ObjC but not for C++
creating build/lib.linux-x86_64-3.6
g++ -pthread -shared -B /usr/local/anaconda/compiler_compat -L/usr/local/anaconda/lib -Wl,-rpath=/usr/local/anaconda/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.6/hellomodule.o -lboost_python3-py36 -o build/lib.linux-x86_64-3.6/hello.cpython-36m-x86_64-linux-gnu.so

Implementation:

$ python3 
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> hello.say_hello("World")
Hello World!

How to know which python so to use?

$ ll /usr/lib/x86_64-linux-gnu/libboost_python*.so
lrwxrwxrwx 1 root root 31 3 Month 618:21 /usr/lib/x86_64-linux-gnu/libboost_python3-py36.so -> libboost_python3-py36.so.1.65.1
lrwxrwxrwx 1 root root 24 3 Month 618:21 /usr/lib/x86_64-linux-gnu/libboost_python3.so -> libboost_python3-py36.so
lrwxrwxrwx 1 root root 30 3 Month 618:21 /usr/lib/x86_64-linux-gnu/libboost_python-py27.so -> libboost_python-py27.so.1.65.1
lrwxrwxrwx 1 root root 24 3 Month 618:21 /usr/lib/x86_64-linux-gnu/libboost_python-py36.so -> libboost_python3-py

Reference material

Posted by richrock on Sun, 29 Dec 2019 11:09:52 -0800