Ok, quick, and hopefully easy question here...
I have a large C++ API that is basically a sockets API with a top level XML type protocol. This is a distributed source package from a company. All of the source code is written into header files (including function implementations) for some odd reason (I guess they figure it is easier for developers to import an H file and not have to worry about compiling multiple cpp files??). The source is also available in C, Java, and .NET.
The application that I am writing is in Python, on Linux, and needs to use this API through Python. My three options seem to be either running the applications separately with a message passing protocol between them, running SWIG (or similar) to generate a Python hook-in, or to reimplement all the source code into Python. Ultimately, I'd like to make it as asynchronous as possible (already using Twisted in other parts of the application).
Using SWIG seems to be the quickest, but there are many custom typed structures that are used in passing in and out of functions, as well as returns from functions, which I have heard can be a bit of an issue with SWIG.
I'd rather not have to write a message protocol as that creates another point of failure and two different source开发者_如何转开发 codes, in two different languages, that I have to manage. Reimplementing the C++ code in Python may ultimately be a good solution, but that is going to require a large amount of effort and time.
My question is, does SWIG seem like a good idea, and if so, will I need to write C++ files to compile that wrap the headers, or should I just forget SWIG and look into something else?
I appreciate any help or thoughts. Thanks.
EDIT: Turns out I misspoke earlier...there is a lot of the source code in header files, but I found a bunch of .lib
and .a
files as well. Is it possible to use SWIG, Boost, or similar on these types of files included? Or do I need to write a top level API over those and try that way? So far my attempts have failed miserably.
SOLUTION: I ended up just implementing my own API straight from Python. Turns out that the protocol spec was not that complex and the C++ libraries actually made it more difficult than it needed to be. I also have the added bonus of building the lib inside an asynchronous framework, instead of having to do threading with the synchronous calls.
SWIG would usually be my preferred solution for problems like this. "Custom typed" structures can be wrapped perfectly sensibly. The thing you will need to watch out for though is the limitations of the SWIG parser if you've got everything in the header files.
You should consider boost_python. It gives you a lot of control over the python/c++ interface and it is actually easy to use. There are some simple tutorials about using boost_python. Since boost_python is c++ itself, you avoid adding a third technology (swig) into your project.
精彩评论