我有以下代码
import ctypes
lib1 = ctypes.cdll.LoadLibrary("./mylib.so")
# modify mylib.so (code generation and compilation) or even delete it
lib2 = ctypes.cdll.LoadLibrary("./mylib.so")
Run Code Online (Sandbox Code Playgroud)
问题是lib2指的是原始共享库,而不是新共享库。如果我在调用之间删除 mylib.so ,则不会出现错误。
使用ctypes._reset_cache()没有帮助。
我如何判断ctypes是否真正从硬盘重新加载库?
我用 C 编写了一个简单的函数,它可以将给定的数字提高到给定的幂。当我在 C 中调用它时,该函数返回正确的值,但是当我在 Python 中调用它时,它返回一个不同的、不正确的值。
我使用以下命令创建了共享文件:
$ gcc -fPIC -shared -o test.so test.c
我尝试了 C 函数的不同配置,其中一些返回预期值,而另一些不返回。例如,当我的函数使用return x*x没有for循环的简单正方形时,它在 Python 中返回了正确的值。
我希望最终能够在 python 中调用一个 C 函数,该函数将返回一个二维 C 数组。
#include <stdio.h>
float power(float x, int exponent)
{
float val = x;
for(int i=1; i<exponent; i++){
val = val*x;
}
return val;
}
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
float power(float x, int exponent)
{
float val = x;
for(int i=1; i<exponent; i++){
val = val*x;
}
return val;
}
Run Code Online (Sandbox Code Playgroud)
我在 C …