1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Ok, It compiles. I get a Warning : : Warning Msg: Using deprecated NumPy API, disable it #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION")
@fmollica Not sure what that's about. Maybe the bindings are using some deprecated functions that's going to be phased out in a future version? Probably want to google and check the numpy doc or the repo's commit log. You should find something about the warning
Ok, It compiles, I believed I could stack path after -I.... I get a Warning : : Warning Msg: Using deprecated NumPy API, disable it #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION") When I finish by the last step : g++ -shared test5.o test5_wrap.o -o _Amod.pyd It gives me plenty of error ` undefined reference to _imp__Py... like if some library where missing
Try g++ -shared test5.o test5_wrap.o -o _Amod.pyd -L C:\Python27\libs -lpython27. You can also pass it in with fully qualified path: g++ -shared test5.o test5_wrap.o -o _Amod.pyd C:/Python27/libs/python27.lib.
And two days of blood and tears come to an end. The right form was indeed g++ -Wall -shared -I C:\Python27\include -I C:\Python27\Lib\site-packages\numpy\core\include\ test5.o test5_wrap.o -o _Amod.pyd -L C:/Python27/libs/ -lpython27. Thank you again. It need to be noted too that one of the bug I get in this SWIG process was the bad encoding of my .i files, they need to be carefully encode in utf-8. Thanks again.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.