在Windows中编译swigpython包装器时,MinGW g++找不到numpy\arrayobject.h

2024-04-23 15:08:47 发布

您现在位置:Python中文网/ 问答频道 /正文

在Debian发行版上,我成功地使用SWIG在C++中构建了Python模块。模块Amod可以在更复杂的python代码中成功导入,并且工作正常。以下是Linux上使用的编译:

swig -c++ -python test5.i
g++ -fPIC -c test5.cpp
g++ -fPIC -c test5_wrap.cxx -I/usr/include/python2.7
g++ -shared test5.o test5_wrap.o -o _Amod.so

但是现在我想在Windows上重新编译它,我不知道我在做什么。在

我安装了nump1.7.0。我在“path”环境变量中添加了swig的path,如swig文档中所述。我还添加了python2.7.5安装和Python.h的路径

我打开windows终端并键入:

^{pr2}$

它编译起来没有问题。那么

g++ -c test5_wrap.cxx 

fatal error: Python.h: no such file or directory。我不明白,Python.h的路径不在path变量中!?所以我打字

g++ -c test5_wrap.cxx -I C:\Python27\include

fatal error: numpy\arrayobject.h: no such file or directory。我也不明白,这个头存在于C:\Python27\Lib\site-packages\numpy\core\include\numpy\。然后我试过了

g++ -c test5_wrap.cxx -I C:\Python27\include C:\Python27\Lib\site-packages\numpy\core\include\numpy\

同样的错误,所以我试着:

g++ -c test5_wrap.cxx -I C:\Python27\include C:\Python27\Lib\site-packages\numpy\core\include\numpy\arrayobject.h

给出一大堆以相同的fatal error: numpy\arrayobject.h: no such file or directory开头的错误。在

怎么解决这个问题?MinGW+SWIG+g++是我编译python模块的正确方向吗?在

非常感谢。在

F.M.公司

编辑:同时,我也尝试用distutils编译这个python模块设置.py公司名称:

# -*- coding: utf-8 -*-
"""
Created on Wed Oct 23 17:01:51 2013

@author: Florian
"""
from distutils.core import setup, Extension


Amod = Extension('_Amod',
                           sources=['test5_wrap.cxx', 'test5.cpp'],
                           )

setup (name = 'Amod',
       version = '0.1',
       author      = "FM",
       description = """Come on""",
       ext_modules = [Amod],
       py_modules = ["Amod"],
       )

和编译:

python setup.py build_ext --compiler=mingw32 --swig-opts="-c++ -I :C/Python27/include -I :C/Python27/Lib/site-packages/numpy/core/include/"

还有这个该死的错误:

running build_ext
building '_Amod' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c test5_wrap.cxx -o build\temp.win32-2.7\Rel
ease\test5_wrap.o
test5_wrap.cxx:3045:31: fatal error: numpy/arrayobject.h: No such file or directory
 #include <numpy/arrayobject.h>

我在google上搜索了我第一次尝试时遇到的错误undefined reference to _imp__Py...,但是人们都在谈论缺少链接的库,我不知道我应该链接哪个库以及为什么。有点超出了我的能力。在


Tags: 模块corenumpyincludecxxerrorfileswig
2条回答

尝试将其更改为-I C:\Python27\include -I C:\Python27\Lib\site-packages\numpy\core\include。gcc知道搜索头的位置。在

在*nix环境中,gcc在查找include头时可能会查找环境变量和其他隐式搜索路径,这可能就是它成功编译的原因。但是,你必须在窗口中显式地指定路径。在

我在cygwin编译c++代码时遇到了同样的问题。解决方案是安装python2-devel和python2-numpy,然后使用命令“python-msite”来查看python的安装目录。使用以下find命令查找头文件:find/usr/lib/python2.7-name“*.h”。头文件位于目录/usr/lib/python2.7/site-packages/numpy/core/include下。在g++命令中使用以下Include选项包括此路径:-I/usr/lib/python2.7/site-packages/numpy/core/Include。在

相关问题 更多 >