软硬件环境
- ubuntu 18.04 64bit
- windows 10 64bit
- Python 3.5.1
- GCC 4.9
- visual studio
前言
最近在做python3
开发中,碰到了一个问题,需要通过调用C
的一个动态链接库来获取相应的值。扒了扒网络,动手实践了下,形成此文。
linux版的动态库
写个简单的C
代码,test.c
#include <stdio.h>
#include <stdlib.h>
char * printStr(const char *p,const char *q)
{
printf("%s",p);
printf("%s",q);
return "djstava";
}
通过以下命令编译成动态链接库
gcc -fPIC -shared -o libtest.so test.c
python3中调用
要调用C
库中的函数,需要用到ctypes
这个模块
# -*- coding: utf-8 -*-
__author__ = 'djstava'
from ctypes import *
handle = cdll.LoadLibrary('libtest.so')
func = handle.printStr
func.argtypes = (c_char_p,c_char_p)
func.restype = c_char_p
tmp = handle.printStr("hello".encode("utf-8"),"world".encode("utf-8"))
print(tmp.decode("utf-8"))
程序执行结果
helloworlddjstava
程序解释
func.argtypes = (c_char_p,c_char_p)
func.restype = c_char_p
这2句是分别设置参数数据类型和返回值类型,如果不进行设置,直接调用的话,参数可以正常接收,但是返回值永远是个int
值,传入的字符串参数必须为encode("utf-8")
,否则在c
库中仅会打印为首字符
handle = cdll.LoadLibrary('libtest.so')
ret = handle.printStr("hello".encode("utf-8"),"world".encode("utf-8"))
关于其它数据类型的argtypes
的设置,请查阅参考文献中的链接
windows版的动态库
Visual Studio
编译dll
,在需要抛出的方法前加入__declspec(dllexport)
, 比如下面C
代码
__declspec(dllexport) unsigned int crc32( const unsigned char *s, unsigned int len)
{
unsigned int i;
unsigned int crc32val=0xffffffff;
printf("len==%d\n",len);
for (i = 0; i < len; i ++)
crc32val = crc32_tab[(crc32val ^ s[i]) & 0xff] ^ ((crc32val >> 8)&0x00FFFFFF);
return ~crc32val;
}
然后打开VS X64
工具命令行提示符,进入到C
源码目录,分别执行以下两条命令,第一条命令是生成目标文件.obj
,第二天命令是链接目标文件,生成动态库。
cl /c crc.c
link /dll crc.obj
至此,dll
文件就生成了,它就是我们需要的动态链接库,dll
的调用跟so
的方法一样。