使用 C 头文件错误编译 Cython

Compiling Cython with C header files error

所以我正在尝试用 Cython 包装一些 C 代码。我阅读了应用 Cython 的有关执行此操作的教程 (1, 2),但是这些教程并没有说明如何在用 Cython 包装代码后编译代码,所以我有一个错误说它找不到我的 C 代码。

首先,我的cython脚本("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

我要包装的 C 代码 ("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"]
                            )])

) 

我的错误:

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

我阅读了这个堆栈溢出线程 Using Cython To Link Python To A Shared Library

但跟随它给了我不同的错误。如果我尝试将 rmsd.h 放入源中,它会说它无法识别文件类型。

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

这看起来很有希望,但我不确定如何使用它。

请帮忙!

首先它必须找到包含文件,rsmd.h。您需要将可以找到此 header 的路径添加到 include_dirs 参数中。关于丢失文件的错误应该消失。

然后您还需要包含通过编译该 C 代码获得的库。如果是 librsmd.a,您可以将 'rsmd' 添加到 libraries 参数。此外,您可能需要一个 library_dirs 参数,其中包含可以找到该库的路径。