Python nontrivial C ++ extension

I have a fairly large C ++ library with several sub-libraries that support it, and I need to turn all this into a python extension. I use distutils because it should be cross-platform, but if there is a better tool, I am open to suggestions.

Is there a way to get distutils to compile sub-libraries first and link them when it creates an extension from the main library?

+4
source share
1 answer

I only do this with the massive C ++ library in our product. There are several tools that can help you automate the task of writing bindings: the most popular is SWIG , which has been around for a while, is used in many projects and, as a rule, works very well.

The biggest thing against SWIG (in my opinion) is that the C ++ SWIG code base itself is pretty cool, to say the least. It was written before STL and has its own semi-dynamic type system, which is now just old and creaky. It doesn't really matter if you don't need to get stuck and make some changes to the kernel (I tried to add doxygen β†’ docstring once), but if you ever do that, good luck! People also say that the generated SWIG code is not so efficient that it can be true, but for me, I never find SWIG calls to be a bottleneck enough to worry about it.

There are other tools you can use if SWIG doesn't float on your boat: boost.python is also popular and can be nice if you already use boost libraries in your C ++ code. The disadvantage is that it is heavy at compile time, as it is almost entirely based on the C ++ pattern.

Both of these tools require you to do some work up to determine what will be shown and how it will be done. For SWIG, you provide interface files that are similar to C ++ headers but separated, and with some additional directives, to tell SWIG how to translate complex types, etc. Writing these interfaces can be tedious, so you may need to look at something like pygccxml to help you automatically generate them for you.

The author of this package actually wrote another extension that you might like: py ++ . This package has two functions: it can automatically generate binding definitions, which can then be sent to boost.python to create python bindings: this is basically a complete solution for most people. You might want to start there if you do not have special or complex requirements for a meeting.

Some other questions that may be helpful as a reference:

You can also find this comparison of binding binding tools for Python. As Alex points out the comments, though, its quite old now, but at least gives you some idea of ​​the landscape ...

In terms of how to manage the assembly, you might want to take a look at a more advanced built-in tool that distutils: if you want to stick with Python, I would highly recommend Waf as a framework (others will tell you SCons is the way to go, but trust me it is slow , like hell: I was already there and back!) ... it takes a little training, but when you hug him, he is extremely powerful. And since this is pure Python, it integrates perfectly with any other Python code that you have as part of the build process (say, for example, you use Py ++ at the end) ...

+10
source

All Articles