【发布时间】:2018-04-03 16:38:40
【问题描述】:
这很棘手(至少对我来说 :-),也许不可行。但我试着问你。
我有这个 c 共享库:
#include <stdio.h>
#include <stdlib.h>
static int variable = -666;
int get_value() {
return variable;
}
void print_pointer_to_get_value() {
printf("pointer_to_get_value: %p\n", &get_value);
}
以这种方式编译(在 Linux 上):
gcc -fPIC -c -O2 shared.c && gcc -shared -o shared.so shared.o
现在我加载库并调用 print_pointer_to_get_value():
>>> import ctypes
>>> so = ctypes.cdll.LoadLibrary('./shared.so')
>>> so.print_pointer_to_get_value()
pointer_to_get_value: 0x7f46e178f700
我想从 ctypes 中获取由 print_pointer_to_get_value() 打印的 get_value 函数的实际地址(整数形式)。 我的最终目标是将该地址移动到 Cython 模块并在“nogil”Cython 函数中调用该函数。我需要在运行时加载 .so 库,因此我无法编译将其链接到库的 Cython 模块。
谢谢 1000。
【问题讨论】: