5.2.1 Accessing C Functions in Separate Files

It is also easy to access C functions defined in separate *.c files. Here's an example. Create files "test.c" and "test.spyx" in the same directory with contents:

The pure C code: "test.c"

int add_one(int n) {
  return n + 1;
}

The Cython code: "test.spyx":

cdef extern from "test.c":
    int add_one(int n)

def test(n):
    return add_one(n)

Then the following works:

sage: attach "test.spyx"
Compiling (...)/test.spyx...
sage: test(10)
11

If an additional library "foo" is needed to compile the C code generated from a Cython file, add the line "clib foo" to the Cython source. Similarly, an additional C file "bar" can be included in the compilation with the declaration "cfile bar".

See About this document... for information on suggesting changes.