How to get boost.python tutorial example to link w

2019-05-05 03:55发布

问题:

I want to use boost.python with python 3.2+ (preferably 3.4) and Visual Studio 2010.

When I try to make the libs\python\example\tutorial example against any Python3 (I have tested 3.0, 3.2 and 3.4) it doesn't link (see below). When I link it against 2.7 it works.

The only change I make between attempts is updating user-config.jam in my home directory.

So it works when user-config.jam is:

#  MSVC configuration
using msvc : 10.0 ;
# Python configuration:
using python : 2.7 : C:\\Python27 : C:\\Python27\\include : C:\\Python27\\libs ;

When I run bjam I get:

C:\Boost\boost_1_55_0\libs\python\example\tutorial>bjam
link.jam: No such file or directory
...patience...
...patience...
...found 1678 targets...
...updating 8 targets...
compile-c-c++ bin\msvc-10.0\debug\hello.obj
hello.cpp
msvc.link.dll bin\msvc-10.0\debug\hello_ext.pyd
   Creating library bin\msvc-10.0\debug\hello_ext.lib and object bin\msvc-10.0\debug\hello_ext.exp
   msvc.manifest.dll bin\msvc-10.0\debug\hello_ext.pyd
   common.copy boost_python-vc100-gd-1_55.dll
   ..\..\..\..\bin.v2\libs\python\build\msvc-10.0\debug\boost_python-vc100-gd-1_55.dll
           1 file(s) copied.
common.copy hello_ext.pyd
bin\msvc-10.0\debug\hello_ext.pyd
           1 file(s) copied.
capture-output bin\hello.test\msvc-10.0\debug\hello
           1 file(s) copied.
**passed** bin\hello.test\msvc-10.0\debug\hello.test
...updated 8 targets...

If I change user-config.jam to:

#  MSVC configuration
using msvc : 10.0 ;
# Python configuration:
using python : 3.4 : C:\\Python34 : C:\\Python34\\include : C:\\Python34\\libs ;

and to bjam clean and then bjam I get:

C:\Boost\boost_1_55_0\libs\python\example\tutorial>bjam
link.jam: No such file or directory
...patience...
...patience...
...found 1685 targets...
...updating 9 targets...
compile-c-c++ bin\msvc-10.0\debug\hello.obj
hello.cpp
msvc.link.dll bin\msvc-10.0\debug\hello_ext.pyd
   Creating library bin\msvc-10.0\debug\hello_ext.lib and object bin\msvc-10.0\debug\hello_ext.exp
   hello.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct _object * __cdecl boost::python::detail::init_module(struct PyModuleDef &,void (__cdecl*)(void))" (__imp_?init_module@detail@python@boost@@YAPAU_object@@AAUPyModuleDef@@P6AXXZ@Z) referenced in function _PyInit_hello_ext
   bin\msvc-10.0\debug\hello_ext.pyd : fatal error LNK1120: 1 unresolved externals

           call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86 >nul
link /NOLOGO /INCREMENTAL:NO /DLL /DEBUG /MACHINE:X86 /MANIFEST /subsystem:console /out:"bin\msvc-10.0\debug\hello_ext.pyd" /IMPLIB:"bin\msvc-10.0\debug\hello_ext.lib" /LIBPATH:"C:\Python34\libs"   @"bin\msvc-10.0\debug\hello_ext.pyd.rsp"
           if %ERRORLEVEL% NEQ 0 EXIT %ERRORLEVEL%

...failed msvc.link.dll bin\msvc-10.0\debug\hello_ext.pyd bin\msvc-10.0\debug\hello_ext.lib bin\msvc-10.0\debug\hello_ext.pdb bin\msvc-10.0\debug\hello_ext.pdb...
...removing bin\msvc-10.0\debug\hello_ext.lib
...removing bin\msvc-10.0\debug\hello_ext.pdb
common.copy boost_python-vc100-gd-1_55.dll
..\..\..\..\bin.v2\libs\python\build\msvc-10.0\debug\boost_python-vc100-gd-1_55.dll
           1 file(s) copied.
...skipped <p.>hello_ext.pyd for lack of <pbin\msvc-10.0\debug>hello_ext.pyd...
...failed updating 3 targets...
...skipped 1 target...
...updated 2 targets...

Looking online I find a couple of references to this error but no solutions... Here is the best link:

http://lists.boost.org/boost-build/2011/06/25147.php

I have seen many people talking about using Python3 and boost.python so I must be missing something... Do I need to compile boost specifically for Python 3 somehow?

Help?

/Robert

回答1:

So through trial and error I found a way that works.

I am not able to recompile a boost library from Python27 to Python34 but if I start from a clean area (i.e freshly 7zipped) and do the following it works (all in a Visual Studio 2010 command prompt):

  1. Make sure you have a user-config.jam file in your home directory with the following content:

    #  MSVC configuration
    using msvc : 10.0 ;
    # Python configuration:
    using python : 3.4 : C:\\Python34 : C:\\Python34\\include : C:\\Python34\\libs ;
    
  2. cd C:\Boost\boost_1_55_0

  3. bootstrap
  4. b2 toolset=msvc-10.0 --build-type=complete --with-python --libdir=C:\Boost\lib\i386 install
  5. cd C:\Boost\boost_1_55_0\libs\python\example\tutorial
  6. set lib=c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\LIB;c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\ATLMFC\LIB;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib;C:\Boost\lib\i386;C:\Python34\libs (i.e add Boost Lib to path)
  7. bjam
  8. Modify hello.py to att () around print:

    import hello_ext
    print(hello_ext.greet())
    
  9. python hello.py prints hello, world (Note that python here is python 3.4.1 my default install)

So what I still don't know how to solve is how to recompile boost and/or how to support two python version at the same time. Ideas and suggestions are welcomed but at the moment I only need Python3 so I will not investigate that.

Another thing to note is that the pre-compiled headers found online all seem to be 2.7 only. I hope these steps helps someone else!