Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2095794
  • 博文数量: 229
  • 博客积分: 7217
  • 博客等级: 上校
  • 技术积分: 3224
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-19 17:23
个人简介

个人主页https://xugaoxiang.com,微信公众号: Dev_Club 或者搜索 程序员Club

文章分类

全部博文(229)

文章存档

2017年(1)

2016年(20)

2015年(23)

2013年(1)

2012年(23)

2011年(68)

2010年(62)

2009年(31)

分类: Python/Ruby

2016-03-29 22:43:44

软硬件环境

  • OS X EI Capitan
  • Python 3.5.1
  • GCC 4.9

前言

最近在做python3开发中,碰到了一个问题,需要通过调用C的一个动态链接库来获取相应的值。扒了扒网络,动手实践了下,形成此文。

准备C动态库

源码test.c

#include 
#include 

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的设置,请查阅参考文献中的链接。

参考文献

阅读(1980) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~