读书人

运用VC++2010扩展python

发布时间: 2012-10-14 14:55:08 作者: rapoo

使用VC++2010扩展python

使用VC++2010扩展python


简介:这里介绍使用VC2010编译C语言编写的python扩展模块,hdlc模块。Hdlc是高级数据链路控制(High-LevelData Link Control或简称HDLC),用于无界流数据上确保用户数据包的完整性。该模块接口应该是用hdlc_pkt = hdlc.pack(buf)把无界流数据buf打包成一个hdlc数据包hdlc_pkt,使用pkt,passlen = hdlc.unpack(buf)把hdlc_pkt数据包还原为pkt数据包,passlen是在buf上读取过的数据长度。

VC新建项目,选用win32的win32项目,输入python的模块名hdlc(源码文件见文档附录)

运用VC++2010扩展python

运用VC++2010扩展python

确定后在选择里选择dll,空项目

运用VC++2010扩展python

运用VC++2010扩展python

复制c文件到项目目录下,然后在VC添加文件。

运用VC++2010扩展python运用VC++2010扩展python

完成源码文件的添加

运用VC++2010扩展python

运用VC++2010扩展python

配置项目属性:(release版本,不要debug版本)

1. 配置属性》常规,把目标文件扩展名改为.pyd

运用VC++2010扩展python运用VC++2010扩展python

2. 添加python头文件目录与库目录

在项目属性的VC++目录页面,添加python的库目录和头文件目录

运用VC++2010扩展python运用VC++2010扩展python

3. 去除预编译,在c/c++的预编译头,不适用预编译头

运用VC++2010扩展python运用VC++2010扩展python

4. 选择release版本,然后编译,生成-》生成hdlc

运用VC++2010扩展python运用VC++2010扩展python

完成python的C语言扩展模块的编译。

使用扩展模块

Projects\hdlc\Release\hdlc.pyd这个pyd文件复制到python安装目录的dlls目录下,即可在python中import使用。

运用VC++2010扩展python

运用VC++2010扩展python

测试该模块

测试脚本

运用VC++2010扩展python

附录

头文件hdlc.h

#compile so file for python CC = gccCFLAG = -Wall -o2INCLUDE = -I/usr/include/python2.6/ all: example.so %.o:%.c    ${CC} ${CFLAG}${INCLUDE} -fpic -c $< -o $@ example_obj=example.o wrap.oexample.so: ${example_obj}    ${CC}${CFLAG} -shared $^ -o $@ .PYONY: cleanclean:    rm *.so *.o



读书人网 >C++

热点推荐