Python3 中提供了 ctypes 模块,它支持与 C 兼容的数据类型,可以用来加载 C/C++ 动态库。
C代码
test.h
1
| extern int add(int, int);
|
test.c
1
2
3
4
5
| #include "test.h"
int add(int arg1, int arg2)
{
return arg1 + arg2;
}
|
C++代码
test.h
1
2
3
4
5
6
7
8
9
| class Foo
{
public:
int add(int arg1, int arg2)
{
return arg1 + arg2;
}
};
extern "C" int add_wrapper(int, int);
|
test.cpp
1
2
3
4
5
6
| #include "test.h"
int add_wrapper(int arg1, int arg2)
{
Foo obj;
return obj.add(arg1, arg2);
}
|
生成动态库
1
2
3
| # 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中调用
1
2
3
| from ctypes import cdll
lib = cdll.LoadLibrary("./libtest.so")
print(lib.add_wrapper(2, 3))
|