user1056255
user1056255

Reputation: 91

Makefile for C program that uses numpy extensions

Please what is the simplest / most elegant way of how to determine correct paths for numpy include as they are present on target system ? And then use it by make command ? At the moment I am using

gcc ... -I/usr/include/python2.7/ -I/usr/lib/python2.7/site-packages/numpy/core/include/numpy/

and I would like to have those two includes automatically selected based on system on which the build is perfromed on.

It seems like I can get the second include like this:

python -c "import numpy; print numpy.__path__[0] + 'core/include/numpy/'"

but I am not sure about the first one and even if I was I still wouldn't be sure how to best use it from makefile (in an easy / elegant way)

Upvotes: 6

Views: 1103

Answers (2)

Brian Larsen
Brian Larsen

Reputation: 1756

numpy.get_include() is the easiest/best way to get the includes. If your C extension module uses numpy then in Extension you have to use include_dirs=[numpy.get_include()]. Why numpy.get_include() doesn't seem to have any documentation I don't know.

Then you can do as user1056255 suggests but just a bit better...

CFLAGS = $(shell python-config --includes) $(shell python -c "import numpy; print '-I' + numpy.get_include()")

Upvotes: 3

user1056255
user1056255

Reputation: 91

So after some googling and experimentig this is what I came up with:

CFLAGS = $(shell python-config --includes) $(shell python -c "import numpy; print '-I' + numpy.__path__[0] + '/core/include/numpy/'")

Not sure how reliable it is but it seems to be working on all machines so far.

I had to use $(shell ...) for setting up make variables from shell output as method proposed by mux didn't work for me

Upvotes: 0

Related Questions