I have a library and some head files, no c++ source code, I want to use it with python. I tried py++, but gccxml report error. I tried swig, but some many "undefined symbol" errors. Are there some smart tools can do such things automatically?
3 Answers
You could try using boost python
You'd need to create a simple wrapper dll that links to your original library, containing code similar to this (assuming you wanted to export a class called LibraryClass with 2 functions, foo & bar)
#include <librarytowrap.h>
#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(Library)
{
class_<LibraryClass>("LibraryClass")
.def("foo", &LibraryClass::foo)
.def("bar", &LibraryClass::bar)
;
}
You might be able to use the automatic code generator to read the C++ definitions in the header files and do the hard work for you, but according to the boost python page this is no longer maintained, so I'm unsure how well it works.

- 9,369
- 36
- 59
-
the automatic code generator uses gccxml, but gccxml report that there's some error in the head file. – WhatisThat Nov 15 '11 at 05:17
-
You could try posting a seperate question about the error you're getting from gccxml. Maybe someone could help with that. – obmarg Nov 15 '11 at 10:15
This approach requires a bit a bit of work, and might not be feasible if you have a lot of functions, that you want to wrap/the functions use a lot of c++ intrinsic types as arguments/return values.
If that is not the case, you can define some c-wrapper functions, with the "extern c" keyword in front of them, with pure c arguments and return types. Compile and link the "wrapper functions" to your c++ library. Then you can use the ctypes module (from python) to call your wrapper functions, which then in turn call the c++ functions in your library.

- 123
- 7