注意事项:
python是64位的要下载64位编译器,否则dll位数不对将报错。笔者下载了mingw64,sourceforge下载地址:
https://sourceforge.net/projects/mingw-w64/files/
向下翻直到看到上图,win10的选x86_64-win32-seh。解压到喜欢的安装目录,并添加目录下的/bin文件夹至环境变量
编译单个文件,例如编译test.c,在cmd中键入
gcc -shared -o test.dll test.c
.c文件编写时,需要共享的方法前加上__declspec(dllexport),例如
__declspec(dllexport) int test()
在python中
import ctypes
dll = ctypes.WinDLL("./test.dll")
print (dll.test())
即可调用。