building boost python examples using Visual Studio 2008
I'm using Boost Python library to create python extensions to my C++ code. I'd like to be able to invok开发者_Go百科e from python the 'greet' function from the C++ code shown below:
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
And the python code :
import hello_ext
print hello_ext.greet()
I've managed to do this using the bjam (hello_ext.pyd is generated and it works nice), but now I'd like to build it using Visual Studio 2008. A hello.dll gets built (but neither hello_ext.dll nor any .pyd). After invoking my python code I get an error:
ImportError: No module named hello_ext.
After renaming the hello.dll to hello.pyd or hello_ext.pyd, I get another ImportError: Dll load failed
How can I build the correct .pyd file using VS 2008?
Firstly, make sure you only try to import the release version from Python; importing the debug version will fail because the runtime library versions don't match. I also change my project properties so that the release version outputs a .pyd file:
Properties >> Linker >> Output:
$(OutDir)\$(ProjectName).pyd
(I also create a post-build action to run unit tests from python)
Next, make sure you define the following in your stdafx.h file:
#define BOOST_PYTHON_STATIC_LIB
Lastly, if you have more than one python version installed, make sure that you're importing the right version of python.h (in Tools >> Options >> Projects and Solutions >> VC++ Directories >> Include Files).
The error ImportError: Dll load failed usually means that your .pyd module depends on other DLLs that couldn't be found - often msvc*.dll. You may want to try opening the .pyd file in Notepad and searching for ".dll". Then check if all of the DLL dependencies exist in your directory or PATH.
Or use Dependency Walker which will find missing dependencies for you
Even though this is a question issued several years ago(still not easy to find solution), but I meet the same problem today, and after hours searching, finally I found a feasible solution.
- The reason is just as simple as which is noticed by @AndiDog, the .pyd file you build depends on some other .dll;
- In my case, It's boost_python-vc120-mt-1_58.dll under the folder [C++ boost folder]/stage/lib/
So, what I do is to copy this file, and paste it under the .pyd file folder, and then my python can properly import the project I build .
maybe there are some other solutions, that is build your project not depend on dynamic library, use static library instead. some of the source said to define BOOST_PYTHON_STATIC_LIB in VS Preprocessor, then your project will not depend on dynamic library(I am a new C++er), but be sure you have build libboost_python-vcXXX-mt-1_58.dll in boost.
- to define Preprocessor, the route is:C/C++->Preprocessor->Preprocessor Definitions->edit BOOST_PYTHON_STATIC_LIB
Please make sure you have flag -lpython26
(if you are using python2.6) and filename should be hello_ext.pyd
in your case.
精彩评论