C++ API as an Python Extension Module - c++

Can APIs written in C or C++ always be made into a library, given source code, and consequently be called from Python, due to the intrinsic callable nature of an API?
In other words, given a C/C++ API can you run a setup.py script using distutils on the source and effectively use the C/C++ API from python programs?

It's a bit more involved than running a setup.py script, but there are tools to wrap C/C++ libraries so you can use them from Python. Some popular ones are:
Cython - you write Python-style code, but you can cimport C libraries and call their functions. Cython code is then translated to C and compiled. There's an unsupported tool called cython-codegen to automate writing a simple wrapper.
boost.python - For wrapping C++ libraries.
SWIG
ctypes - Load compiled libraries and call them directly from Python. ctypesgen tries to automatically write a wrapper based on header files.
Certain projects use other tools to generate Python bindings, and they could probably be reused (with varying amounts of effort). PyQt4 uses SIP, PySide (another Qt binding) has shiboken, and GTK et al. have Gobject introspection.

It's not exactly that simple, but basically you can do that using SWIG, which will generate the necessary wrappers to bind your python and C/C++ code. http://www.swig.org/Doc1.3/Python.html

There is no intrinsic or automated mechanism for binding a C++ API to Python. It must be done manually. The best tool I've seen for this purpose is Boost Python.
Of course, given the clean mapping between C++ Object Oriented programming and Python OO code, it shouldn't be too difficult to create a script which parses C++ headers and produces the binding declarations. GCC-XML will give you an xml representation of parsed headers, which can be easily queried by a python script to produce the bindings. That said, there's probably a great deal of detail I've not anticipated which could prevent this approach from working.

Related

Extending Python in C

I have successfully extended my python code in C following this:
Call a Python function from within a C program
I have compiled the code and created a .dll. However, when I opened it with the dependencywalker I have seen that it still requires the python code. I want the dll to be standalone, so it doesn't depend on the script .py. I thought that using the tag -static in g++ would be enough but it doesn't work for me.
The Python documentation has a chapter on Extending and Embedding Python with C.
With good enough coding conventions this can by applied to C++ code. You will use extern "C" to declare functions coded in C++ callable from C.
Be careful to avoid throwing a C++ exception to Python code.
On Linux, see also dlopen(3) and dlsym(3) and the C++ dlopen mini howto.
Python is open source. You should consider downloading its C source code and studying it.
Be aware of the GIL in Python. Multi-threading C++ code will be tricky.
Consider also generating C or C++ code with tools like RefPerSys or ANTLR, or writing in Python your C or C++ code generator.
Perhaps use static source code analyzers (like Frama-C or Bismon) on your C source code.
Be aware that C and C++ are different programming languages. See this and n1570 and n3337 or better.
PS. For Bismon or RefPerSys please contact me by email to basile#starynkevitch.net and basile.starynkevitch#cea.fr, but mention the URL of your question here in your email.

Automatically generate go bindings to a c++ library

I am working on a program which from a c++ code generates a .so and bindings for multiple languages (C, C++, Haskell, Java, Php, Python...) and I'm trying to add support for golang.
I think the best way to get that result is to either use the C++ or the C version and create go bindings, but this has to be done automatically. I've tried using Swig but I didn't manage to get it working (likely because we're including a .so and not a .cc) and I've only found old documentation (this uses go tool 6g).
I've also tried creating bindings with cgo which doesn't work with C++, and I couldn't get it working with the C bindings (again, most likely because of the .so step).
At the end, I must generate another .so with a few exported functions, and which must be standalone which makes compiling the C++ version and talking with it through a pipe (almost?) impossible.
The functions to export are often things like:
void init_game(void);
void play_turn(void);
void end_game(void);
You can find a c++ header here and the auto-generated interfaces to C: interface.hh and interface.cc and prologin.h.
An example source code of lib.so can be found here.

Calling a C++ shared Library in Ruby

I have a shared library "mylib.so" which was written in C++ and I would like to make an application in Ruby that calls the library functions.
I researched and found FFI (https://github.com/ffi/ffi) and it only works with a C library. With Rice (https://rubygems.org/gems/rice/versions/2.1.0) only I found tutorial that I need to change the source code of the library, but I do not have access to this code.
Is there any way to implement a C++ library in my Ruby code to use its functions?
When you say you don't have access to the code, you mean that you do not have access to the headers of the library? If you do not have access to the headers defining classes, prototyping functions and exposing some API, then you wouldn't be able to actually bind this library to any other segment of code, whether the latter is written in c++, python, ruby or whatever.
If you do have access to the headers of the library, then you can easily use rice-ruby to build a ruby wrapper - by following the instructions here. You need only the headers and an up-to-date version of your library in order to do a proper wrap. Feel free to better define your problem and I'll try to help.

boost.build Vs boost.python

Context -- Trying to use Boost.Python set of C++ libraries to interface with C++ code.
Main idea is to test C++ code (.so files) by using them like python from a QA point of view.
Questions now;
BOOST_PYTHON_MODULE wrapper, do we really need to include in every .cpp to be interfaced from Python? Say we have test.cpp, can't we have Boost wrapper written test_qa.cpp so that actual dev code is not changed in the process?
Looked Boost.org site to get more clarity, what is the difference between Boost.Build and Boost.Python?
From the Boost Build documentation:
Boost.Build is an easy way to build C++ projects, everywhere.
From the Boost Python documentation:
... a C++ library which enables seamless interoperability between C++ and the Python programming language
I would say the difference between these two parts of Boost should be pretty obvious.

Using C++ bindings with GObject Introspection

I decided I want to use the Goffice library in my project. But I write it in C++, so I prefer to have a C++ class interface, just like I use gtkmm and not GTK+ directly.
The documentation (see link above) says I can use GObject Introspection. So I started reading anout it. I read and read and read, and I just couldn't understand how to use any binding of GOffice. I looked for a goffice gi-repository/typelib file on my system, and in the list of files installed by PackageKit. Found nothing. I checked in PackageKit if goffice or goffice-devel packages depend on the gobject introspection package. Maybe they depend indirectly, but they don't depend on it directly (otherwise I'd see it on the list).
I tried and tried, but I couldn't find a resource which could simply explain how to take a library written in GObject, such as GOffice, and use it on another language, e.g. Python, or in my case C++. Of course, I can use the C functions directly, but the point is that I want to have an interface similar to gtkmm.
(I use GNU/Linux, writing a desktop application with gtkmm and GNU build system, goffice version 0.10)
There is currently no GObject Introspection tool for C++. You can see a list of users at https://live.gnome.org/GObjectIntrospection/Users.
Based on one of GOffice's automake files, the GIR name GOffice is GOffice-0.10, so you should expect $(pkg-config --variable=girdir gobject-introspection-1.0)/GOffice-0.10.gir and $(pkg-config --variable=typelibdir gobject-introspection-1.0)/GOffice-0.10.typelib, but it's possible your distribution's packages don't include those files, in which case you might want to consider filing a bug.
As for documentation on how to use GObject Introspection for Python, you should check out the PyGObject site. They link to the The Python GTK+ 3 Tutorial, which should help you get a feel for how to use PyGObject. As for other languages, the documentation will vary depending on the language and implementation.
gtkmm, glibmm, and other -mm libraries currently still use the gmmproc tool to generate bindings for GObject-based libraries. This tool is older than GObject Introspection (GI is considered stable from GTK+ 3) and requires manual work on writing headers with special macros which will be used by the tool to generate C++ source code. For more details and a how-to see Wrapping C Libraries with gmmproc.
As for automatic C++ binding generation using GObject Introspection, I've found only one WIP: gi-mm.
Alternatively there are GObject Consume and Smoke-GObject which both can be used to integrate GObjects with C++ through Qt framework.
cppgir is GObject-Introspection C++ binding wrapper generator (also it is listed here).
It is lightweight (straight binding), optionally it can be used inline (header-only), therefore the program can directly link to any GObject-based library (GTK, GStreamer, etc).
You can read more information from README and documentation.