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

所以我正在尝试创建一个使用我编写的.c文件的Cython模块。 该.c文件需要一个特殊的链接选项(要编译它,我需​​要gcc -o mycode mycode.c -lfftw3f )。 我可能只是在Cython中重写我的.c文件,但我想知道如何做到这一点。

我正在使用fftw3,并且在编译时,如果你想使用浮点版本,你需要在.c文件中使用-lfftw3f选项#include

我的setup.py如下所示:

 from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext sourcefiles = ['mycode_caller.pyx', 'mycode.c'] ext_modules = [Extension("myext", sourcefiles, libraries=['fttw3f'])] setup( name = 'My Extension', cmdclass = {'build_ext': build_ext}, ext_modules = ext_modules ) 

我创建了一个名为mycode.h的头文件,如下所示,并包含transform()函数的原型,该函数在mycode.c定义:

 #include  #include  #ifndef FOURIER_H_INCLUDED #define FOURIER_H_INCLUDED fftwf_complex** transform(float** in, int length); #endif 

我的Cython文件mycode_caller.pyx看起来像这样:

 import numpy as np cimport numpy as np cdef extern from "stdlib.h": void free(void* ptr) void* malloc(size_t size) cdef extern from "fftw3.h": struct fftwf_complex: pass cdef extern from "fourier.h": fftwf_complex** transform(float** in_arr, int length) cdef float** npy2c_float2d(np.ndarray[float, ndim=2] a): cdef float** a_c = malloc(a.shape[0] * sizeof(float*)) for k in range(a.shape[0]): a_c[k] = &a[k, 0] return a_c cpdef test_transform(data): nparr = np.zeros([14, 31]) c_array = npy2c_float2d(nparr) ans = transform(c_array, 31) 

当我运行python setup.py build_ext --inplace ,它构建正常,但如果我尝试导入它,它将声明以下内容:

ImportError: ./myext.so: undefined symbol: fftwf_execute

由于在编译期间没有将-lfftw3f选项传递给gcc而发生此错误。 我该如何解决这个问题? 没有办法在.c源文件中指定链接器命令吗? 我是否需要告诉Cython.Distutils以某种方式使用此选项? 谢谢您的帮助!

编辑:所以,我在我的setup.py文件中添加了libraries=[fttw3f] ,现在它在构建时抛出了一个错误:

 gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/emodsp.o build/temp.linux-x86_64-2.7/fourier.o -lfftw3f -o /home/carson/Documents/Caltech/Senior/Winter/art89/Project/openepoc/emodsp.so /usr/bin/ld: /usr/local/lib/libfftw3f.a(alloc.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC /usr/local/lib/libfftw3f.a: could not read symbols: Bad value collect2: error: ld returned 1 exit status error: command 'gcc' failed with exit status 1 

只需使用Extensionlibraries选项:

 Extension("myext", sourcefiles, libraries = ['fftw3f'], library_dirs = ['/path/to/fftw/libs'])