rafferino
rafferino

Reputation: 31

Building Python-C Extension using CFFI, but Setuptools does not include custom header files in build

I'm trying to use the CFFI package in Python to create a Python interface for already existing C-code.

I am able to compile a C library by following this blog post. Now I want to make it so that this python library is available without any fancy updates to the sys.path.

I found that maybe creating a distribution through Python's setuptools setup() function would accomplish this and I got it to mostly work by creating a setup.py file as such

import os
import sys

from setuptools import setup, find_packages

os.chdir(os.path.dirname(sys.argv[0]) or ".")

setup(
    name="SobelFilterTest",
    version="0.1",
    description="An example project using Python's CFFI",
    packages=find_packages(),
    install_requires=["cffi>=1.0.0"],
    setup_requires=["cffi>=1.0.0"],
    cffi_modules=[
        "./src/build_sobel.py:ffi",
        "./src/build_file_operations.py:ffi",
    ],
)

, but I run into this error

build/temp.linux-x86_64-3.5/_sobel.c:492:19: fatal error: sobel.h: No such file or directory

From what I can tell, the problem is that the sobel.h file does not get uploaded into the build folder created by setuptools.setup(). I looked for suggestions of what to do including using Extensions() and writing a MANIFEST.in file, and both seem to add a relative path to the correct header files:

MANIFEST.in
setup.py
SobelFilterTest.egg-info/PKG-INFO
SobelFilterTest.egg-info/SOURCES.txt
SobelFilterTest.egg-info/dependency_links.txt
SobelFilterTest.egg-info/requires.txt
SobelFilterTest.egg-info/top_level.txt
src/file_operations.h
src/macros.h
src/sobel.h

But I still get the same error message. Is there a correct way to go about adding the header file to the build folder? Thanks!

Upvotes: 3

Views: 997

Answers (1)

Gijs Wobben
Gijs Wobben

Reputation: 2060

It's actually not pip that is missing the .h file, but rather the compiler (like gcc). Therefore it's not about adding the missing file to setup, but rather make sure that cffi can find it. One way (like mentioned in the comments) is to make it available to the compiler through environment variables, but there is another way.

When setting the source with cffi you can add directories for the compiler like this:

from cffi import FFI

ffibuilder = FFI()
ffibuilder.set_source("<YOUR SOURCE HERE>", include_dirs=["./src"])

# ... Rest of your code
"""

Upvotes: 0

Related Questions