首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ImportError: Cython和gcc-5

ImportError: Cython和gcc-5
EN

Stack Overflow用户
提问于 2015-11-10 15:22:01
回答 1查看 190关注 0票数 1

我想通过使用Cython在Python中使用一些C-函数。在这里,我注意到如果我使用GCC-5编译C代码(为了使用Cilk),nm在结果*..so函数中列出了更少的入口:

代码语言:javascript
复制
0000000000201030 B __bss_start
0000000000201030 b completed.6973
                 w __cxa_finalize@@GLIBC_2.2.5
00000000000005c0 t deregister_tm_clones
0000000000000630 t __do_global_dtors_aux
0000000000200df8 t __do_global_dtors_aux_fini_array_entry
0000000000201028 d __dso_handle
0000000000200e08 d _DYNAMIC
0000000000201030 D _edata
0000000000201038 B _end
00000000000006a8 T _fini
0000000000000670 t frame_dummy
0000000000200df0 t __frame_dummy_init_array_entry
00000000000006b8 r __FRAME_END__
0000000000201000 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
0000000000201031 B __gnu_lto_slim
0000000000201032 B __gnu_lto_v1
0000000000000570 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000200e00 d __JCR_END__
0000000000200e00 d __JCR_LIST__
                 w _Jv_RegisterClasses
00000000000005f0 t register_tm_clones
0000000000201030 d __TMC_END__

我所有的自写功能都丢失了。此外,我得到了错误。

代码语言:javascript
复制
ImportError: dynamic module does not define init function (initcython_wrapper)

我怎么才能解决这个问题,为什么?

pyx-file的内容:

代码语言:javascript
复制
from libcpp cimport bool as bool_t
cdef extern from "complex.h":
    pass

cimport numpy as np

# if you want to use the Numpy-C-API from Cython
# (not strictly necessary for this example, but good practice)
np.import_array()

# cdefine the signature of our c function
cdef extern from "GNLSE_RHS.h":
    void compute(int size, double *a, double *b)
    void speedcompute(int size, double *a, double *b)
    void test_fft(int size, double complex *a, double complex *b)

# create the wrapper code, with numpy type annotations
def compute_func(np.ndarray[double, ndim=1, mode="c"] in_array not None,
                     np.ndarray[double, ndim=1, mode="c"] out_array not None):
    compute(in_array.shape[0], <double*> np.PyArray_DATA(in_array),
                <double*> np.PyArray_DATA(out_array))

def speedcompute_func(np.ndarray[double, ndim=1, mode="c"] in_array not None,
                    np.ndarray[double, ndim=1, mode="c"] out_array not None):
    speedcompute(in_array.shape[0], <double*> np.PyArray_DATA(in_array),
                <double*> np.PyArray_DATA(out_array))   

def fft_func(np.ndarray[complex, ndim=1, mode="c"] in_array not None,
                np.ndarray[complex, ndim=1, mode="c"] out_array not None):
    test_fft(in_array.shape[0], <complex*> np.PyArray_DATA(in_array),
            <complex*> np.PyArray_DATA(out_array))   

以及setup.py-file中的命令行选项:

代码语言:javascript
复制
os.environ["CC"] = "gcc-5" 
os.environ["CXX"] = "g++-5"
# The configuration object that hold information on all the files
# to be built.
config = Configuration('', parent_package, top_path)
config.add_extension(name='cython_wrapper',
                     sources=['cython_wrapper.pyx', 'GNLSE_RHS.c'],
                     # libraries=['m'],
                     include_dirs=[numpy.get_include(),  "/opt/intel/compilers_and_libraries_2016.0.109/linux/mkl/include"],
                     #libraries=["m", "pthread", "gsl", "gslcblas"],
                     depends=['GNLSE_RHS.c'],
                     extra_compile_args=["-DMKL_ILP64 -mavx -msse4.2 -msse3 -msse2 -m64 -Ofast -flto -march=native -funroll-loops -std=gnu99"],
                     extra_link_args=["-Wl,--start-group /opt/intel/compilers_and_libraries_2016.0.109/linux/mkl/lib/intel64/libmkl_intel_ilp64.a /opt/intel/compilers_and_libraries_2016.0.109/linux/mkl/lib/intel64/libmkl_core.a /opt/intel/compilers_and_libraries_2016.0.109/linux/mkl/lib/intel64/libmkl_sequential.a -Wl,--end-group -lpthread -lm -ldl -lfftw3 -lfftw3_threads"])
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-18 12:02:04

我可以解决我的问题:背后的原因不是编译器,而是选项-flto,它在与gcc-5一起使用时生成奇怪的代码。删除此选项将生成可用代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33633410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档