在Qt Creator中,我创建了一个小库(在Python项目的“Squaring”子文件夹中):
平方.h:
int squaring(int a);
平方.c:
#include "squaring.h"
int squaring(int a){
return a * a;
}
在Eclipse中,我创建了一个Python项目,试图使用这个库(according to the instructions from the official website):
cSquaring.pxd:
cdef extern from "Squaring/squaring.h":
int squaring(int a)
函数.pix:
cimport cSquaring
cpdef int count(int value):
return cSquaring.squaring(value)
设置.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
setup(ext_modules=cythonize([Extension("Functions", ["Functions.pyx"])]))
main.py:
from Functions import count
if __name__ == '__main__':
data = 1
returned = count(data)
print(returned)
使用(无任何错误)完成了C代码的编译:
python3 setup.py build_ext -i
但当我在执行时运行main.py时,我会感到恐惧:
File “/home/denis/.p2/pool/plugins/org.python.pydev.core_7.3.0.201908161924/pysrc/pydevd.py”, line 2643, in <module>
main()
File “/home/denis/.p2/pool/plugins/org.python.pydev.core_7.3.0.201908161924/pysrc/pydevd.py”, line 2636, in main
globals = debugger.run(setup, None, None, is_module)
File “/home/denis/.p2/pool/plugins/org.python.pydev.core_7.3.0.201908161924/pysrc/pydevd.py”, line 1920, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File “/home/denis/.p2/pool/plugins/org.python.pydev.core_7.3.0.201908161924/pysrc/pydevd.py”, line 1927, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File “/home/denis/.p2/pool/plugins/org.python.pydev.core_7.3.0.201908161924/pysrc/_pydev_imps/_pydev_execfile.py”, line 25, in execfile
exec(compile(contents+“\n”, file, ‘exec’), glob, loc)
File “/home/denis/eclipse-workspace/ConnectionWithCPlusPlus/main.py”, line 1, in <module>
from Functions import count
ImportError: /home/denis/eclipse-workspace/ConnectionWithCPlusPlus/Functions.cpython-37m-x86_64-linux-gnu.so: undefined symbol: squaring
而另一个使用Cython的项目(在那里我没有创建C库,而是直接在Cython上编写代码)运行良好
有什么问题
你在Cython中包含了头文件,但是你从来没有告诉过它实现,也就是定义函数的库。您需要针对编译C源代码生成的库进行链接,如Cython docs中所述
在
Functions.pyx
中,必须在顶部添加这样的注释相关问题 更多 >
编程相关推荐