到目前为止,我已经尝试了互联网上提到的所有解决方案。
我有一个 Python 代码,为了加快速度,我希望我的代码在 C 函数中运行繁重的计算。 我已经编写了这个 C 函数。
然后,为了共享库,我在终端中这样做了:
gcc -shared -Wl,-install_name,testlib.so -o testlib.so -fPIC myModule.c
没有返回错误。问题;当我尝试在 python 中启动 C 函数时出现。让我们考虑以下 C 中的简单函数:
int multiplier(int a, int b)
{
int lol = 0;
lol = a*b;
return lol;
}
我启动 python3 (3.5.2),然后:
import ctypes
zelib = ctypes.CDLL("/Users/longeard/Desktop/Codes/DraII/testlib.so",ctypes.RTLD_GLOBAL)
库应该准备好在 python 中使用:
res = zelib.multiplier(2,3)
当这样做时,它工作并且 python 返回
6
问题是,我想使用的函数(我使用的乘数函数只是为了示例)应该将 float 作为输入并返回一个 float 。但是,如果我现在考虑与以前相同的乘数函数,但使用 float :
float multiplier(float a, float b)
{
float lol = 0.0;
lol = a*b;
return lol;
}
我使用 gcc 重新编译,我重新导入 ctypes 并重新做 ctypes.CDLL,我在 python3 中做:
zelib.multiplier(ctypes.c_float(2),ctypes.c_float(3))
(types.c_float 在这里将 python 中的 2 转换为 C 中的 float ),python 将返回:
2
这很奇怪,因为如果我在函数中添加一个 printf 来打印 lol,python 将打印:
6.0
但有时仍会返回 2 或 18。即使我 printf 并返回相同的变量“lol”。
我尝试了很多方法,但都没有用。请问有人有想法吗?谢谢。
最佳答案
需要指定函数的restype
、argtypes
:
zelib = ctypes.CDLL('...')
zelib.multiplier.restype = ctypes.c_float # return type
zelib.multiplier.argtypes = [ctypes.c_float, ctypes.c_float] # argument types
根据 Specifying the required argument types (function prototypes) :
It is possible to specify the required argument types of functions exported from DLLs by setting the
argtypes
attribute.
和Return types在 ctypes
module documentation :
By default functions are assumed to return the C int type. Other return types can be specified by setting the
restype
attribute of the function object.
# without specifying types
>>> import ctypes
>>> zelib = ctypes.CDLL('testlib.so')
>>> zelib.multiplier(2, 3)
0
# specifying types
>>> zelib.multiplier.restype = ctypes.c_float
>>> zelib.multiplier.argtypes = [ctypes.c_float, ctypes.c_float]
>>> zelib.multiplier(2, 3)
6.0
关于python - 在 Python 中使用 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40102274/