[英]Building boost::python module with SCons under Windows
I'm currently trying too build a boost::python module using scons. 我目前正在尝试使用scons构建boost :: python模块。 I've managed to find some code snippets on the Web and stitch them together so that it builds correctly on Linux.
我设法在Web上找到一些代码片段并将它们拼接在一起,以便可以在Linux上正确构建。 But I have some major problems on Windows (using Visual Studio 2013 compiler).
但是我在Windows上有一些主要问题(使用Visual Studio 2013编译器)。 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.obj
RunGA.pyd 运行GA.pyd
RunGA.exp RunGA.exp
RunGA.lib 运行GA.lib
To import the module I need a .dll file instead of .lib, but I'm not really sure how to do this properly. 要导入模块,我需要一个.dll文件而不是.lib文件,但我不确定如何正确执行此操作。
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.' 编辑04.06.15:尝试导入RunGA模块(通过“导入RunGA”)时,出现以下错误消息:“ ImportError:DLL加载失败:找不到指定的模块。”
EDIT2 04.06.15: 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.
原来,项目目录和系统路径中缺少带有boost python的.dll文件,RunGA.pyd依赖于此文件。 Thank you all for your help.
谢谢大家的帮助。
You are generating the .dll you need, it has just been renamed to a .pyd file. 您正在生成所需的.dll,它刚刚已重命名为.pyd文件。 It also probably needs to have a leading
_
in it's filename before your python module will load it. 在您的python模块加载文件名之前,可能还需要在文件名中包含前导
_
。 You can get that by appending the following line to your SConstruct... 您可以通过将以下行添加到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
. 建筑的顺序,你可能看到的是
RunGA.obj
从正在修建RunGA.cpp
文件,那么库RunGA.lib
被建造,然后RunGA.exp
和RunGA.lib
用于创建您的RunGA.pyd
。 That is, it is exporting a .dll
for you, but then renaming it to .pyd
. 也就是说,它将为您导出
.dll
,然后将其重命名为.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
. 这与您在Linux上看到的不同,在Linux上您可能只看到
RunGA.o
, RunGA.py
和_RunGA.so
。 On Linux the dynamic library needs to named _MODULE.so
, and on Windows, it needs to be named _MODULE.pyd
. 在Linux上,动态库需要命名为
_MODULE.so
,而在Windows上,则需要命名为_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
第一个python必须找到并加载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 假设您具有“ C:\\ path \\ RunGA.pyd”,并且RunGA的依赖项都在系统路径上
set PYTHONPATH=C:\path
python
>>>import RunGA
Then the pyd (which is actually a dll) has to successfully load. 然后pyd(实际上是dll)必须成功加载。 You will almost certainly have a "boost_python{version}.dll" dependency, for example.
例如,您几乎肯定会具有“ boost_python {version} .dll”依赖性。 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).
您配置了build env来查找它,但是它可能不在系统路径上找不到任何可执行文件(例如python,它尝试导入模块,但找不到其其他依赖项)。
Programs like http://www.dependencywalker.com/ are very handy for determining what dlls are needed to load another dll. 像http://www.dependencywalker.com/这样的程序对于确定加载另一个dll所需的dll非常方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.