在 64 位操作系统上构建 C->Python 库

发布于 2025-01-05 22:00:06 字数 2209 浏览 1 评论 0原文

我有一个用 Swig 包装的第三方库,这样我就可以用 Python 与它对话。这适用于 32 位操作系统/Python/第三方库。最近,我从第三方收到了这些库的 64 位版本,因此我想重建这些库以利用这一点(当然,使用 64 位 Python 和 64 位操作系统)。但是,当我尝试导入库时,我收到此错误:

Traceback (most recent call last):
  File "camera.py", line 1, in <module>
    import memhandler
  File "C:\neo\memhandler.py", line 26, in <module>
    _memhandler = swig_import_helper()
  File "C:\neo\memhandler.py", line 22, in swig_import_helper
    _mod = imp.load_module('_memhandler', fp, pathname, description)
ImportError: DLL load failed: %1 is not a valid Win32 application.

(memhandler 是我正在构建的 SWIG 库之一) 我的搜索发现其他人由于 32 位/64 位不匹配而出现此错误,因此这似乎表明存在某种体系结构不匹配,但我无法找出问题所在。

我使用 Visual Studio 2008 的命令行来链接 Python 2.7 64 位;那里没有版本不匹配的情况(换句话说,Python 2.7 64 位是使用相同的编译器构建的)。我过去用不同的程序成功地做到了这一点;遗憾的是,我无法通过比较来判断这个有什么不同。我正在使用 64 位编译器(通过在开始之前运行“vcvarsall.bat amd64”)。

这是我的 Makefile:

PYROOT = C:\Python27x64
PYINCLUDE = /I$(PYROOT)\include /I$(PYROOT)\Lib\site-packages\numpy\core\include
PYLIBS = /LIBPATH:$(PYROOT)\libs python27.lib
ANDORLIBS = "Andor SDK3\atcorem.lib"
LIBS = $(PYLIBS) $(ANDORLIBS)

CXXFLAGS = /MD /EHsc /DWIN32 /D_WINDOWS /DNOPCH /O2 $(PYINCLUDE)

# We have two separate SWIG libraries to build here; one calls most of the
# Andor functions; the other does some intermediary work for us before
# calling certain special Andor functions. We also have to compile the
# intermediary library ourselves.
ANDOROBJS = neo_wrap.obj
WRAPOBJS = memhandler.obj
WRAPSWIGOBJS = memhandler_wrap.obj

ANDORTARGET = _neo.pyd
WRAPTARGET = _memhandler.pyd

all: $(ANDORTARGET) $(WRAPTARGET)

$(ANDORTARGET): $(ANDOROBJS)
    link /dll /DEBUG /NOLOGO /out:$@ /SUBSYSTEM:WINDOWS $(ANDOROBJS) $(LIBS)

$(WRAPTARGET): $(WRAPOBJS) $(WRAPSWIGOBJS)
    link /dll /DEBUG /NOLOGO /out:$@ /SUBSYSTEM:WINDOWS $(WRAPOBJS) $(LIBS)
    link /dll /DEBUG /NOLOGO /out:$@ /SUBSYSTEM:WINDOWS $(WRAPSWIGOBJS) $(LIBS) memhandler.obj

swig:
    C:\swigwin-2.0.4\swig.exe -c++ -python neo.i
    C:\swigwin-2.0.4\swig.exe -c++ -python memhandler.i

clean:
    del *ilk *lib *pdb *obj *exp *wrap.cxx *pyd *pyd.manifest

有什么想法吗?我唯一能想到的是我实际上没有 64 位库;我不知道如何判断 DLL/LIB 文件是 64 位还是 32 位。

I have a third-party library that I have wrapped with Swig so that I can talk to it in Python. This is working for 32-bit OS/Python/third-party libs. Recently I received 64-bit versions of those libraries from the third party, so I want to rebuild the libraries to take advantage of this (using 64-bit Python and 64-bit OS, of course). However, I'm getting this error when I try to import the libraries:

Traceback (most recent call last):
  File "camera.py", line 1, in <module>
    import memhandler
  File "C:\neo\memhandler.py", line 26, in <module>
    _memhandler = swig_import_helper()
  File "C:\neo\memhandler.py", line 22, in swig_import_helper
    _mod = imp.load_module('_memhandler', fp, pathname, description)
ImportError: DLL load failed: %1 is not a valid Win32 application.

(memhandler is one of the SWIG libraries I'm building)
My searches have turned up other people who had this error due to 32-bit / 64-bit mismatches, so this would seem to indicate some kind of architecture mismatch, but I'm having trouble figuring out where it's going wrong.

I'm using Visual Studio 2008's command line to link against Python 2.7 64-bit; there's no version mismatch there (Python 2.7 64-bit was built with the same compiler, in other words). I've done this successfully in the past with a different program; sadly I can't tell by comparing this against it what's different about this one. I am using the 64-bit compiler (by running "vcvarsall.bat amd64" before starting).

Here's my Makefile:

PYROOT = C:\Python27x64
PYINCLUDE = /I$(PYROOT)\include /I$(PYROOT)\Lib\site-packages\numpy\core\include
PYLIBS = /LIBPATH:$(PYROOT)\libs python27.lib
ANDORLIBS = "Andor SDK3\atcorem.lib"
LIBS = $(PYLIBS) $(ANDORLIBS)

CXXFLAGS = /MD /EHsc /DWIN32 /D_WINDOWS /DNOPCH /O2 $(PYINCLUDE)

# We have two separate SWIG libraries to build here; one calls most of the
# Andor functions; the other does some intermediary work for us before
# calling certain special Andor functions. We also have to compile the
# intermediary library ourselves.
ANDOROBJS = neo_wrap.obj
WRAPOBJS = memhandler.obj
WRAPSWIGOBJS = memhandler_wrap.obj

ANDORTARGET = _neo.pyd
WRAPTARGET = _memhandler.pyd

all: $(ANDORTARGET) $(WRAPTARGET)

$(ANDORTARGET): $(ANDOROBJS)
    link /dll /DEBUG /NOLOGO /out:$@ /SUBSYSTEM:WINDOWS $(ANDOROBJS) $(LIBS)

$(WRAPTARGET): $(WRAPOBJS) $(WRAPSWIGOBJS)
    link /dll /DEBUG /NOLOGO /out:$@ /SUBSYSTEM:WINDOWS $(WRAPOBJS) $(LIBS)
    link /dll /DEBUG /NOLOGO /out:$@ /SUBSYSTEM:WINDOWS $(WRAPSWIGOBJS) $(LIBS) memhandler.obj

swig:
    C:\swigwin-2.0.4\swig.exe -c++ -python neo.i
    C:\swigwin-2.0.4\swig.exe -c++ -python memhandler.i

clean:
    del *ilk *lib *pdb *obj *exp *wrap.cxx *pyd *pyd.manifest

Any ideas? About the only thing I can think of is that I don't actually have 64-bit libraries; I don't know how to tell if a DLL/LIB file is 64-bit or 32-bit though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文