简体   繁体   中英

Building boost::python module with SCons under Windows

I'm currently trying too build a boost::python module using scons. I've managed to find some code snippets on the Web and stitch them together so that it builds correctly on Linux. But I have some major problems on Windows (using Visual Studio 2013 compiler). Here's the code:

import distutils.sysconfig, os,sys


def TOOL_BOOST_DISTUTILS(env):    
vars = distutils.sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS', 'CCSHARED', 'LDSHARED', 'SO')
for i in range(len(vars)):
    if vars[i] is None:
        vars[i] = ""
(cc, cxx, opt, basecflags, ccshared, ldshared, so_ext) = vars    
env.AppendUnique(CPPPATH=[distutils.sysconfig.get_python_inc(), "D:/boost-vs2013/include/boost-1_57"])

if sys.platform == "win32":
    print "Configuring for Windows" 
    env.AppendUnique(CXXFLAGS=["/MD","/EHsc"])      
else:
    env.AppendUnique(LIBS="boost_python")
    env.AppendUnique(CXXFLAGS =["-std=c++11"])

env['SHLIBSUFFIX']=so_ext
env.AppendUnique(LIBPATH = [distutils.sysconfig.get_python_inc()+"/libs", distutils.sysconfig.PREFIX+"/libs","D:/boost-vs2013/lib"])

Default('.')
env=Environment(tools=['default', TOOL_BOOST_DISTUTILS],TARGET_ARCH = 'x86')
env.SharedLibrary(target='RunGA', source=['RunGA.cpp'])

During the build, following files are created:

RunGA.obj

RunGA.pyd

RunGA.exp

RunGA.lib

To import the module I need a .dll file instead of .lib, but I'm not really sure how to do this properly.

EDIT 04.06.15: When trying to import the RunGA module (by 'import RunGA'), I get the following error message: 'ImportError: DLL load failed : The specified module could not be found.'

EDIT2 04.06.15:

I've managed to solve the problem. It turned out that the .dll file with boost python was missing from the project's directory and system paths and RunGA.pyd depended on it. Thank you all for your help.

You are generating the .dll you need, it has just been renamed to a .pyd file. It also probably needs to have a leading _ in it's filename before your python module will load it. You can get that by appending the following line to your SConstruct...

if sys.platform == "win32":
    print "Configuring for Windows" 
    env.AppendUnique(CXXFLAGS=["/MD","/EHsc"])
    env.Replace(LDMODULEPREFIX='_',
                SHLIBPREFIX='_')

The order of building that you probably see is the RunGA.obj being built from your RunGA.cpp file, then the library RunGA.lib gets built, and then the RunGA.exp and RunGA.lib are used to create your RunGA.pyd . That is, it is exporting a .dll for you, but then renaming it to .pyd .

This is different from what you see on Linux, where you probably just end up with RunGA.o , RunGA.py , and a _RunGA.so . On Linux the dynamic library needs to named _MODULE.so , and on Windows, it needs to be named _MODULE.pyd .

You already are building the correct file, but your question indicates you don't know how to import.

There are two steps. First python has to find and load the dll https://docs.python.org/2/tutorial/modules.html

assuming you have "C:\\path\\RunGA.pyd" and RunGA's dependencies are all on the system path

set PYTHONPATH=C:\path
python
>>>import RunGA

Then the pyd (which is actually a dll) has to successfully load. You will almost certainly have a "boost_python{version}.dll" dependency, for example. You configured the build env to find it, but it is probably not on the system path for any executable to find (eg python, which tries to import the module, but can't find its other dependencies).

Programs like http://www.dependencywalker.com/ are very handy for determining what dlls are needed to load another dll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM