Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 845
user1056255 Avatar asked Jan 26 '12 14:01

user1056255


People also ask

Can you use NumPy in C?

NumPy is written in C, and executes very quickly as a result. By comparison, Python is a dynamic language that is interpreted by the CPython interpreter, converted to bytecode, and executed. While it's no slouch, compiled C code is always going to be faster.

What are NumPy C extensions?

C extensions are C code that can be compiled and linked to a shared library that can be imported like any Python module and you can call specified C routines like they were Python functions.

Can I use SciPy in C?

I believe the vast majority of NumPy and SciPy is written in C and wrapped in Python for ease of use. It probably depends what you're doing in any of those languages as to how much overhead there is for a particular application.


1 Answers

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()")
like image 100
Brian Larsen Avatar answered Oct 12 '22 22:10

Brian Larsen