gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:57 25 4
gpt4 key购买 nike

在我的 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 上重新编译它,但我不确定自己在做什么。

我安装了 numpy 1.7.0。和带有 g++ 的 MinGW,并且我在“PATH”环境变量中添加了 swig 的路径,如 SWIG 文档中所述。我还添加了我的 Python 2.7.5 安装路径和 Python.h。

我打开 Windows 终端并输入:

swig -c++ -Wall -python test5.i
g++ -c -Wall test5.cpp

它编译没有问题。然后

g++ -c test5_wrap.cxx 

fatal error :Python.h:没有那个文件或目录。我不明白,Python.h 的路径不在 PATH 变量中!?所以我输入

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

fatal error :numpy\arrayobject.h:没有这样的文件或目录。我也不明白,这个 header 存在于 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 模块编译的正确方向吗?

非常感谢。

调频

编辑:与此同时,我还尝试使用此 setup.py 使用 distutils 编译此 python 模块:

# -*- 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>

我用谷歌搜索了第一次尝试时遇到的错误 undefined reference to _imp__Py...,但人们谈论缺少链接库,我看不出我应该链接哪个库以及为什么。这有点超出我的技能。

最佳答案

尝试将其更改为 -I C:\Python27\include -I C:\Python27\Lib\site-packages\numpy\core\include。需要这些路径,以便 gcc 知道在哪里搜索包含 header 。

在 *nix 环境中,gcc 在查找包含 header 时可能会查找环境变量和其他隐式搜索路径,这可能就是它编译成功的原因。但是,在 Windows mingw 上,您必须明确指定这些路径,以便它看起来在正确的位置。

关于c++ - 在 Windows 中编译 SWIG python 包装器时,MinGW g++ 找不到 numpy\arrayobject.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19531642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com