使用 C 头文件编译 Cython 错误

roh*_*anp 5 c c++ linker distutils cython

所以我尝试用 Cython 包装一些 C 代码。我阅读了有关执行此操作的应用 Cython 的教程(12),但是这些教程没有过多说明如何在使用 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
Run Code Online (Sandbox Code Playgroud)

我试图包装的 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
}
Run Code Online (Sandbox Code Playgroud)

安装程序.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"]
                            )])

) 
Run Code Online (Sandbox Code Playgroud)

我的错误:

calcRMSD.c:269:10: fatal error: 'rsmd.h' file not found
#include "rsmd.h"
Run Code Online (Sandbox Code Playgroud)

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

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

如何将自定义 C(其本身需要特殊链接选项来编译)与 Cython 链接?

这看起来有点有前途,但我不知道如何使用它。

请帮忙!

sth*_*sth 4

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

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