How to properly link to a C library in cython C++ build

190 views Asked by At

I have a compiled C library htslib in my module top-level directory, this is first built using ./configure; make outside of python before running python setup.py install. I am trying to use headers listed in htslib. I have cython scripts e.g. script.pyx which wrap supporting header files cpp_header.h, but these are compiled using C++ via the setup script. Setup of the python module runs without problems, but at runtime htslib symbols are missing, I get ImportError: foo.cpython-37m-x86_64-linux-gnu.so: undefined symbol: hts_close. My directory structure is:

foo/
    htslib/  # c library
        objectfiles.o
        htslib/
           headers.h 
        cram/
           more_headers.h
           more_objects.o
    foo/
       script.pyx
       cpp_header.h    # c++ 
    setup.py

In my setup script I list some libraries needed for htslib, some library_dirs, include_dirs, and extra_compile_args for building the Extension:

root = os.path.abspath(os.path.dirname(__file__))
htslib = os.path.join(root, "htslib") 

libraries = [htslib]  

library_dirs = [htslib, numpy.get_include(), root, "htslib", "foo"]
include_dirs = [numpy.get_include(), root, htslib]
for item in ["htslib", "cram"]:
    include_dirs.append(os.path.join(htslib, item))

ext_modules.append(Extension(f"foo.script",
                             [f"foo/script.pyx"],
                             libraries=libraries,
                             library_dirs=library_dirs,
                             include_dirs=include_dirs,
                             extra_compile_args=extras,
                             define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")],
                             language="c++"))

Additionally, I also add htslib to the package_data option in setup:

setup(...
      packages=["foo"],
      package_data={"foo": ["htslib/*"]},
      ext_modules=cythonize(ext_modules),
      ...
)

Inside the cpp_header.h file the htslib headers are included as:

#include "../htslib/htslib/sam.h"

Im trying this on unix. Would any one know how I can get this working?

0

There are 0 answers