But first I was eager to test of wrapping some simple C++ code with Boost library and calling the resulting library from Python and all of that within one instance of Visual Studio 2013.
This is a result of my effort on experimental HelloWorld library incorporating:
- Python (libs and includes being referenced from Visual Studio)
- Visual Studio 2013
- Python Tools for Visual Studio
- Boost 1.5.5(compiled and lib and includes directories being referenced from Visual Studio)
The main things for library to be callable from Python that are really important and are not aforementioned in Boost documentation:
- Resulting DLL has to be renamed to PYD. Python interpreted does not load DLL (At least I was not successful in convincing an interpreter to do that)
- Resulting PYD library has to have the same name like parameter of BOOST_PYTHON_ MODULE(...). So in my case I have hello as a parameter so correspondingly my compiled module is named hello.pyd.
- PYD file has to be available/findable for Python interpreter. Verify that your file is in corresponding %PYTHONPATH%. Otherwise Python's import hello (in my case) would cause runtime errors.
#define BOOST_ALL_DYN_LINK #define BOOST_LIB_DIAGNOSTIC #define BOOST_PYTHON_STATIC_LIB #include <boost/python.hpp> using namespace boost::python; class World { public: void set(std::string msg) { this->msg = msg; } std::string greet() { return msg; } std::string msg; friend std::ostream &operator<<(std::ostream &o, World const &w) { o << w.msg ; return o; } }; BOOST_PYTHON_MODULE(hello) { class_<World>("World") .def("greet", &World::greet) .def("set", &World::set) .def(self_ns::str(self_ns::self)) ; }
The sample code is available on github:
Once I have had library compiled I was test it within Visual Studio's Python interpreter. (I really like the simplicity of combining C++ and Python in one development tool). That's really cool.
No comments:
Post a Comment