3

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!

rafferino
  • 31
  • 1
  • 1
    You can use `CFLAGS` env var to adjust the include paths, e.g. `CFLAGS="-I$(pwd)/src/" python setup.py build_ext` etc. – hoefling Jul 09 '19 at 19:46
  • Wow I've been stuck on this problem for days thank you so much. Is there a way to upvote a comment? – rafferino Jul 09 '19 at 21:09
  • Glad I could help! I guess you'll be able to upvote comments when gained more reputation; however, upvoting comments is IMO useless anyway, so I wouldn't bother. – hoefling Jul 10 '19 at 13:34

1 Answers1

0

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
"""
Gijs Wobben
  • 1,974
  • 1
  • 10
  • 13