Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python setup script extensions, how do you include a .h file?

So I've got a directory that looks something like this:

 home\
     setup.py
     some_python_file.py
     ext\
         __init__.py
         c_file1.c
         c_file2.c
         ext_header.h

Obviously the header file is necessary to compile the c files, but the problem is that I can't get the setup script to include the header file.

My extension object is something like this:

Extension('ext.the_extension', ['ext/c_file1.c', 'ext/c_file2.c'])

Which works, but doesn't include the header file. If I change it to:

Extension('ext.the_extension', ['ext/c_file1.c', 'ext/c_file2.c', 'ext_header.h'])

It includes the '.h' file but then doesn't build when I run install. Instead it gives and error error: unknown file type '.h' (from 'ext/ext_header.h')

If I include the header file as a data file like this:

data_files=[('ext', ['ext/ext_header.h'])]

it doesn't work at all, the .h file doesn't even make it into the MANIFEST file.

So my queustion is, how do you include this extension with the header file so that python setup.py install will build it correctly?

like image 343
user411133 Avatar asked Nov 23 '10 17:11

user411133


People also ask

How do I import a .h file?

To import a header, use the #include, a preprocessor directive telling the compiler that it should import and process the code before compiling the rest of the code. On a typical C program, it should contain the stdio. h header file, which is the standard header file for input and output streams.

What are .h extension files?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

Does Python use .H files?

No, Python doesn't have header documents nor comparative. Neither does Java, regardless of your suggestion that it does. All things considered, we use "docstrings" in Python to make it simpler to discover and utilize our interfaces (with the builtin help() work).

Can you include header files in header files?

A TL;DR definition: A header file must include the header files that directly define each of the types directly used in or that directly declare each of the functions used in the header file in question, but must not include anything else.


2 Answers

I have a feeling pyfunc is on track for a more standard solution, but I did find another solution on my own. I have no idea if this is a good solution or just a hack, but all I did is add the header file to the MANIFEST.in. The documentation doesn't really make it seem like this is what the MANIFEST.in file is for, but it does work. My MANIFEST.in file now looks like this:

include ext/ext_header.h

Which includes the file and sucessfully compiles when I run python setup.py install

like image 113
user411133 Avatar answered Sep 21 '22 07:09

user411133


From the docs,

module1 = Extension('demo',
                define_macros = [('MAJOR_VERSION', '1'),
                                 ('MINOR_VERSION', '0')],
                include_dirs = ['/usr/local/include'],
                libraries = ['tcl83'],
                library_dirs = ['/usr/local/lib'],
                sources = ['demo.c'])

You should provide the include files via "include_dirs".

Why does this not work for you?

like image 21
pyfunc Avatar answered Sep 23 '22 07:09

pyfunc