Including .o files in Cython Project

Viewed 324

I am trying to make Python bindings to interact with a C object, using Cython. I was given:

parser.o and parser.h

I've made my .pyx and .pxd files to define the methods and types that I'll use from the .h file. When I try to call a method that I defined in my .pxd that mirrors one defined in the .h, I get the error:

undefined symbol: function_name

My setup.py looks like:

from setuptools import setup, Extension
from Cython.Build import cythonize

extensions = [
        Extension(
                'parser',
                ['parser.pyx'],
        )
]
compiler_directives = {'language_level': 3}

setup(
        ext_modules = cythonize(
                extensions,
                **compiler_directives
        )
)

I think I don't know how to include the parser.o files I was given. Any help on how to link the .o file with what I'm trying to do would be great.

1 Answers

I encountered the same problem this morning, and found that the extension needs the additional .o files to be provided through extra_link_args. However, in your case, I have some concern that Cython will clobber your parser.c file. It might help to rename the original parser.c and parser.h to something like parser_c.c and parser_c.h to avoid this; and generate parser_c.o.

from Cython.Build import cythonize

extensions = [
        Extension(
                'parser',
                ['parser.pyx'],
                extra_link_args = ['parser_c.o']
        )
]
compiler_directives = {'language_level': 3}

setup(
        ext_modules = cythonize(
                extensions,
                **compiler_directives
        )
)```

Related