1

I've been researching the topic of using C++ code in Python, but haven't found a generic clean flexible way to wrap C++ library in the Python package.

The question is whether it's possible to use existing complex C++ library to create a regular Python library, that can be called exactly like native Python libraries, such as NumPy or SciPy. If yes, any references would be much appreciated. If there are examples/tutorials available - it would be even more useful.

Thanks

1

1 Answer 1

5

There are many, many ways. Boost Python, http://www.boost.org/doc/libs/1_57_0/libs/python/doc/ , is very C++-specific and exploits C++ templates to the hilt (like all of Boost!-). Part of more general (less C++ specific) approaches include manual C coding of Python extensions, per https://docs.python.org/3/extending/extending.html ; SWIG, per http://www.swig.org/Doc1.3/SWIGPlus.html ; Cython, per http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html ; ... and no doubt others I haven't come across yet.

The very existence of so many strong, actively maintained alternatives, hints that there's no "one size fits all" here! If you're a template wizard I bet you'll swear by Boost; if you're not, I guess you're more likely to swear at it -- and so on, and so forth.

Personally, I tend to end up using Cython (or even just ctypes!-) for experimenting, manual extension coding when I decide I want to do a lot of Python work using a certain C++ library (and performance is crucial) -- and SWIG at work, because that's the standard there. Haven't seriously used Boost in far too long -- a refresh on it goes on my not-so-tiny todo list for when my spare time gets more copious...:-).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.