在Cython中使用C库时,我遇到了一个错误
I get an error, when using C-library with Cython
在Qt Creator中,我创建了一个小库(在Python项目的"Squaring"子文件夹中(:
平方。h:
int squaring(int a);
平方。c:
#include "squaring.h"
int squaring(int a){
return a * a;
}
在Eclipse中,我创建了Python项目,尝试使用这个库(根据官方网站上的说明(:
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时,我会得到ImportError:
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文档中所述。
在Functions.pyx
中,您必须在顶部添加这样的注释。
# distutils: sources = path/to/squaring.c
相关文章:
- C++,我收到一个无法理解的编译器错误
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 删除映射和分割错误中的一个过去结束元素
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- 写一个分数类,我无法弄清楚这些错误
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 一个标头库中的错误
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- 使用Visual Studio在虚幻引擎中创建一个新的类c ++给了我太多的错误
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- 试图修复一个错误,该错误不会让我开始编程其余部分
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 我正在尝试一个傻瓜 C++ 练习,我遇到了一个错误,说类 'GraduateStudent' 没有任何名为 'advisor' 的字段
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 在使用堆栈为下一个最大数字编写代码时面临 SIGSEGV(分段错误)
- LNK2019错误一个DLL链接到MFC DLL
- 错误:一个声明中有多个类型
- 带有两个运算符的C++11模糊错误(一个左值-第二个右值).底部的