使用C生成DLL,并通过Python进行调用
工具Visual Studio 2015, Python 3
方法- 使用VS生成DLL:
#includeextern "C" __declspec(dllexport) int sum(int a, int b) { return a+b; }
- 在Python中进行调用
from ctypes import * # 以下四种调用均可 path_to_dll = "./myTest.dll" pDLL = WinDLL(path_to_dll) # pDLL = windll.LoadLibrary(path_to_dll) # pDLL = cdll.LoadLibrary(path_to_dll) # pDLL = CDLL(path_to_dll) # 调用链接库函数 s = pDLL.sum(1, 2) print(s)
- 注意事项
在C/C++混合编程时,不要忘记extern “C”
#ifdef __cplusplus
extern "C"{
#endif
...
#ifdef __cplusplus
}
#endif



