communicate between python and C++

segfault

I want to create a python module which can have its functions called from a C++ class and call c++ functions from that class

i have looked at boost however it hasn't seemed to make any sense it refers to a shared library (which i have no idea how to create) and i cant fallow the code they use in examples (it seems very confusing)

here is their hello world tutorial (http://www.boost.org/doc/libs/1_55_0b1/libs/python/doc/tutorial/doc/html/index.html#python.quickstart)

Following C/C++ tradition, let's start with the "hello, world". A C++ Function:

char const* greet()
{
   return "hello, world";
}

can be exposed to Python by writing a Boost.Python wrapper:

include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python. Here's a sample Python session:

>>> import hello_ext
>>> print hello_ext.greet()
hello, world

Next stop... Building your Hello World module from start to finish...

could someone please help explain what is being done and most of all how python knows about the C++ file

logc

Python does not know about the C++ file, it will only be aware of the extension module that is compiled from the C++ file. This extension module is an object file, called a shared library. This file has an interface that looks to Python as if it was a normal Python module.

This object file will only exist after you tell a compiler to compile the C++ file and link it with all the libraries it needs. Of course, the first library needed is Boost.Python itself, which must be available on the system where you are compiling.

You can tell Python to compile the C++ file for you, so that you do not need to mess with the compiler and its library flags. In order to do so, you need a file called setup.py where you use the Setuptools library or the standard Distutils to define how your other Python modules are to be installed on the system. One of the steps for installing is compiling all extension modules, called the build_ext phase.

Let us imagine you have the following directories and files:

hello-world/
├── hello_ext.cpp
└── setup.py

The content of setup.py is:

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


hello_ext = Extension(
    'hello_ext',
    sources=['hello_ext.cpp'],
    include_dirs=['/opt/local/include'],
    libraries=['boost_python-mt'],
    library_dirs=['/opt/local/lib'])


setup(
    name='hello-world',
    version='0.1',
    ext_modules=[hello_ext])

As you can see, we are telling Python there is an Extension we want to compile, where the source file is, and where the included libraries are to be found. This is system-dependent. The example shown here is for a Mac OS X system, where Boost libraries were installed via MacPorts.

The content of hello_ext.cpp is as shown in the tutorial, but take care to reorder things so that the BOOST_PYTHON_MODULE macro comes after the definitions of whatever must be exported to Python:

#include <boost/python.hpp>

char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

You can then tell Python to compile and link for you by executing the following on the command line:

$ python setup.py build_ext --inplace
running build_ext
building 'hello_ext' extension
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -pipe -Os -fwrapv -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/opt/local/include -I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c hello_ext.cpp -o build/temp.macosx-10.9-x86_64-2.7/hello_ext.o
/usr/bin/clang++ -bundle -undefined dynamic_lookup -L/opt/local/lib -Wl,-headerpad_max_install_names -L/opt/local/lib/db46 build/temp.macosx-10.9-x86_64-2.7/hello_ext.o -L/opt/local/lib -lboost_python-mt -o ./hello_ext.so

(The --inplace flag tells Python to leave the products of compilation right next to the source files. The default is to move them to a build directory, to keep the source directory clean.)

After that, you will find a new file called hello_ext.dll (or hello_ext.so on Unix) on the hello-world directory. If you start a Python interpreter in that directory, you will be able to import the module hello_ext and use the function greet, as shown in the Boost tutorial.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Communicate between a C++ plugin and a Python program

From Dev

Simplest way to communicate between Python and C# using IPC?

From Dev

Simple way to communicate between C# app and Python app

From Dev

Python: Communicate between two files

From Dev

Communicate between NodeJS and C++

From Dev

C++ to communicate to Python function

From Java

How to communicate between two instances of python interpreter?

From Dev

Communicate between a processing sketch and a python program?

From Dev

Way to use Multiple queue to communicate between two python scripts

From Dev

Python3 webserver communicate between threads for IRC bot

From Dev

How to communicate between my python frame and task bar icon

From Dev

C# - How to communicate between classes using interface with one property

From Dev

How to communicate between ASP.NET & C# Application

From Dev

C# - How to communicate between classes using interface with one property

From Dev

Communicate between two macros

From Dev

How to communicate between Agents?

From Dev

How to communicate between services?

From Dev

Communicate between Packages in Java

From Dev

Communicate between UIAutomation and app

From Dev

How to communicate between fragments?

From Dev

Communicate between Packages in Java

From Dev

Communicate between Controller and ApiController

From Dev

Communicate between two fragments

From Dev

How to Communicate between ViewModels?

From Dev

Communicate between 2 applications

From Dev

What is the fastest way to have C# communicate with Python?

From Dev

How to communicate from python script to C# exposed interface?

From Dev

How to communicate from python script to C# exposed interface?

From Dev

How to communicate between javascript modules

Related Related

  1. 1

    Communicate between a C++ plugin and a Python program

  2. 2

    Simplest way to communicate between Python and C# using IPC?

  3. 3

    Simple way to communicate between C# app and Python app

  4. 4

    Python: Communicate between two files

  5. 5

    Communicate between NodeJS and C++

  6. 6

    C++ to communicate to Python function

  7. 7

    How to communicate between two instances of python interpreter?

  8. 8

    Communicate between a processing sketch and a python program?

  9. 9

    Way to use Multiple queue to communicate between two python scripts

  10. 10

    Python3 webserver communicate between threads for IRC bot

  11. 11

    How to communicate between my python frame and task bar icon

  12. 12

    C# - How to communicate between classes using interface with one property

  13. 13

    How to communicate between ASP.NET & C# Application

  14. 14

    C# - How to communicate between classes using interface with one property

  15. 15

    Communicate between two macros

  16. 16

    How to communicate between Agents?

  17. 17

    How to communicate between services?

  18. 18

    Communicate between Packages in Java

  19. 19

    Communicate between UIAutomation and app

  20. 20

    How to communicate between fragments?

  21. 21

    Communicate between Packages in Java

  22. 22

    Communicate between Controller and ApiController

  23. 23

    Communicate between two fragments

  24. 24

    How to Communicate between ViewModels?

  25. 25

    Communicate between 2 applications

  26. 26

    What is the fastest way to have C# communicate with Python?

  27. 27

    How to communicate from python script to C# exposed interface?

  28. 28

    How to communicate from python script to C# exposed interface?

  29. 29

    How to communicate between javascript modules

HotTag

Archive