English 中文(简体)
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 invoke 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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签