Python3 中提供了 ctypes 模块,它支持与 C 兼容的数据类型,可以用来加载 C/C++ 动态库。
C代码
test.h
extern int add(int, int);
test.c
#include "test.h"
int add(int arg1, int arg2)
{
return arg1 + arg2;
}
C++代码
test.h
class Foo
{
public:
int add(int arg1, int arg2)
{
return arg1 + arg2;
}
};
extern "C" int add_wrapper(int, int);
test.cpp
#include "test.h"
int add_wrapper(int arg1, int arg2)
{
Foo obj;
return obj.add(arg1, arg2);
}
生成动态库
# c语言: gcc -c -fPIC -o test.o test.c
gcc -c -fPIC -o test.o test.cpp
gcc -shared -o libtest.so test.o
python中调用
from ctypes import cdll
lib = cdll.LoadLibrary("./libtest.so")
print(lib.add_wrapper(2, 3))