3

I am trying to compile a module using cython, but the compiler can not find include libraries in an external c file (I use cmath as example, but other libraries that I want such as cstdio, cstdint, cstring have the same issue)

A minimal example is composed of the following 4 simple files:

cfile.c:

#include <cmath>

test_pxd.pxd:

cdef extern from "cfile.c":
    pass

test.pyx:

cimport test_pxd

setup.py:

from distutils.core import Extension, setup
from Cython.Build import cythonize

sources = ['test.pyx']

extension = [Extension('test',sources)]

setup(ext_modules=cythonize(extension,force=True))

If I run the setup.py with:

 python3 setup.py build_ext --inplace

I get the error:

cfile.c:1:17: fatal error: cmath: No such file or directory

It should be noted that just compiling the c file directly, eg with g++ -c cfile.c, does not require any extra linking for the compiler to find these libraries.

How can I make the compiler in cython find the cmath (and other, such as cstdio, cstdint, cstring) libraries in an external c file?

taras
  • 6,566
  • 10
  • 39
  • 50
Jonathan Lindgren
  • 1,192
  • 3
  • 14
  • 31

1 Answers1

0

The definition should be against the header, not the implementation:

cdef extern from "cfile.h":
    pass

If using C++ libraries, must add language='c++' directive to setuptools/cythonize.

cythonize(<..>,
          language="c++",
      )

See also docs.

danny
  • 5,140
  • 1
  • 19
  • 31