Florian M.
Florian M.

Reputation: 321

numpy\arrayobject.h not find by MinGW g++ while compiling SWIG python wrapper in Windows

On my debian distribution, I managed to build a python module in C++ using SWIG. The module Amod can be imported succesfully in more complex python code and work fine. Here the compilation used on 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

But now I want recomplie it on Windows, and I am not sure of what I am doing.

I installed numpy 1.7.0. and MinGW with g++, and I added path for swig in the "PATH" environment variable, as explained in the SWIG doc. I added also path to my Python 2.7.5 installation and to Python.h.

I open the windows terminal and type :

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

It compiles without problems. then

g++ -c test5_wrap.cxx 

fatal error: Python.h: no such file or directory. I don't get it, the path to Python.h isn't in the PATH variable !? so I type

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

fatal error: numpy\arrayobject.h: no such file or directory. I don't get it either, this header exists in C:\Python27\Lib\site-packages\numpy\core\include\numpy\. Then I tried

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

same error and so I tried :

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

give a huge pile of errors beginning with the same fatal error: numpy\arrayobject.h: no such file or directory.

How solve that ? Is MinGW + SWIG + g++ the right direction for my python module compilation ?

Thanks a lot.

F.M

Edit : Meanwhile I tried also to compile this python module with distutils with this setup.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"],
       )

And compiling with :

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

And still this dammit error :

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>

I googled the error undefined reference to _imp__Py... that I got with my first try, but people talk about missing linked library, I don't see which library I should link and why. It's a bit over my skills.

Upvotes: 1

Views: 3262

Answers (2)

user3969377
user3969377

Reputation:

I had the same problem in cygwin while compiling a c++ code. Solution was to install python2-devel and python2-numpy, and then use the command "python -m site" in order to see the python installation directories. Use the following find command to locate the header file: find /usr/lib/python2.7 -name "*.h". The header files are under directory /usr/lib/python2.7/site-packages/numpy/core/include. Include this path with the following include option to the g++ command: -I/usr/lib/python2.7/site-packages/numpy/core/include.

Upvotes: 0

greatwolf
greatwolf

Reputation: 20858

Try changing it to -I C:\Python27\include -I C:\Python27\Lib\site-packages\numpy\core\include. Those paths are needed so gcc knows where to search for the include headers.

In a *nix environment, there are likely environment variables and other implicit search paths gcc looks in when looking for include headers and that's probably why it compiles successfully . On windows mingw, however, you'll have to specify those paths explicitly so it looks in the right place.

Upvotes: 1

Related Questions