简体   繁体   English

使用C头文件编译Cython错误

[英]Compiling Cython with C header files error

So I'm trying to wrap some C code with Cython. 所以我试图用Cython包装一些C代码。 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. 我读读申请用Cython的教程就这样做( 12 ),但这些教程不说太多关于如何编译代码,一旦你用用Cython包裹它,所以我有一个错误说,它不能找到我的C码。

First, my cython script ("calcRMSD.pyx"): 首先,我的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

The C code I am trying to wrap ("rmsd.h"): 我试图包装的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 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 我读了这个堆栈溢出线程使用Cython将Python链接到共享库

but following it gives me different errors. 但是跟随它会给我带来不同的错误。 If I try to put rmsd.h in sources, it says it doesnt recognize the file type. 如果我尝试将rmsd.h放在源代码中,它表示它不识别文件类型。

How to link custom C (which itself needs special linking options to compile) with Cython? 如何将自定义C(它本身需要特殊的链接选项进行编译)与Cython链接?

This looks somewhat promising but im not sure how to use it. 这看起来有点有希望,但我不知道如何使用它。

Please help! 请帮忙!

First of all it has to find the include file, rsmd.h . 首先,它必须找到包含文件rsmd.h You need to add the path where this header can be found to the include_dirs parameter. 您需要将可以找到此标头的路径添加到include_dirs参数中。 The error about the missing file should disappear. 有关丢失文件的错误应该消失。

Then you will additionally need to include the library you get from compiling that C code. 然后,您还需要包含从编译C代码中获得的库。 If that's librsmd.a you would add 'rsmd' to the libraries parameter. 如果是librsmd.a ,则将'rsmd'添加到libraries参数中。 Additionally you might need a library_dirs parameter that contains the path where that library can be found. 此外,您可能需要library_dirs参数,该参数包含可以找到该库的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM