Compiling Cython with C header files error

2019-06-24 01:35发布

问题:

So I'm trying to wrap some C code with Cython. I read read applied Cython's tutorials on doing this (1, 2), but these tutorials do not say much on how to compile the code once you have wrapped it with Cython, and so I have an error saying it can't find my C code.

First, my cython script ("calcRMSD.pyx"):

import numpy as np
cimport numpy as np

cdef extern from "rsmd.h":
    double rmsd(int n, double* x, double* y)

#rest of the code ommited

The C code I am trying to wrap ("rmsd.h"):

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

extern "C" {
  // svd from lapack
  void dgesvd_(char*,char*,int*,int*,double*,int*,double*,double*,int*,double*,
           int*,double*,int*,int*);
}

double rmsd(int n, double* x, double* y)
{
   //code omitted
}

Setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import numpy as np


setup(
    ext_modules = cythonize([Extension("calcRMSD", 
                            sources = ["calcRMSD.pyx"],
                            include_dirs = [np.get_include()],
                            libraries = ["dgesvd"]
                            #extra_compile_args = ["-I."],
                            #extra_link_args = ["-L./usr/lib/liblapack.dylib"]
                            )])

) 

My error:

calcRMSD.c:269:10: fatal error: 'rsmd.h' file not found
#include "rsmd.h"

I read this stack overflow thread Using Cython To Link Python To A Shared Library

but following it gives me different errors. If I try to put rmsd.h in sources, it says it doesnt recognize the file type.

How to link custom C (which itself needs special linking options to compile) with Cython?

This looks somewhat promising but im not sure how to use it.

Please help!

回答1:

First of all it has to find the include file, rsmd.h. You need to add the path where this header can be found to the include_dirs parameter. The error about the missing file should disappear.

Then you will additionally need to include the library you get from compiling that C code. If that's librsmd.a you would add 'rsmd' to the libraries parameter. Additionally you might need a library_dirs parameter that contains the path where that library can be found.