python中调用C++写的动态库
一、环境:Windows XP + Python3.2
1. dll对应的源文件(m.cpp):
#include <stdio.h> extern "C"
{
_declspec(dllexport) int add(int a, int b)
{
return a+b;
} _declspec(dllexport) void print_sum(unsigned long ulNum)
{
while(ulNum != )
{
printf("The ulNum is : %u\n", ulNum--);
}
}
}
2. python源程序:
# coding=GBK from ctypes import *
import time if __name__ == '__main__':
time_begin = time.clock() #dll = CDLL("d.dll") # 加载dll方式一
dll = cdll.LoadLibrary("d.dll") # 加载dll方式二
print(dll.add(2, 6)) # 调用dll中add方法
dll.print_sum(100) # 调用dll中print_sum方法 t = time.clock() - time_begin # 计算时间差
print("Use time: %f" %t) # 打印耗时时间
运行输出:
E:\Program\Python>del The ulNum is :
The ulNum is :
The ulNum is :
...........
The ulNum is :
The ulNum is :
Use time: 0.003853 E:\Program\Python>
二、环境:Fedora12 + Python2.6
1. 动态库源文件(a.cpp):
#include <stdio.h> extern "C"
{
int add(int a, int b)
{
return a+b;
} void print_sum(unsigned long ulNum)
{
while(ulNum != )
{
printf("The ulNum is : %u\n", ulNum--);
}
}
}
编译指令:g++ -shared -o liba.so a.cpp
2. python源程序(del.py):
#!/usr/bin/env python
# coding=UTF-8 from ctypes import *
import time if __name__ == '__main__':
time_begin = time.clock() dll = CDLL("./liba.so") # 加载dll方式一(默认在系统lib库路径下查找.so文件)
#dll = cdll.LoadLibrary("./liba.so") # 加载dll方式二
print(dll.add(2, 6)) # 调用dll中add方法
dll.print_sum(10000) # 调用dll中print_sum方法 t = time.clock() - time_begin # 计算时间差
print("\nUse time: %s" %t) # 打印耗时时间
注意:Linux上用Python加载动态库时默认是从系统lib路径下是查找库文件的。所以在python中加载当前路径下的动态库的话,路径要写“./liba.so",否则会提示动态库文件找不到!
http://blog.csdn.net/joeblackzqq/article/details/7405992
http://blog.csdn.net/magictong/article/details/3075478(更详细的说明)
http://blog.csdn.net/oracleot/article/details/3851512(使用SWIG实现C++导出python接口的配置)
python中调用C++写的动态库的更多相关文章
- Qt打开外部程序和文件夹需要注意的细节(Qt调用VC写的动态库,VC需要用C的方式输出函数,否则MinGW32编译过程会报错)
下午写程序中遇到几个小细节,需要在这里记录一下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 QProcess *process = new QProcess(this ...
- 使用ctypes在Python中调用C++动态库
使用ctypes在Python中调用C++动态库 入门操作 使用ctypes库可以直接调用C语言编写的动态库,而如果是调用C++编写的动态库,需要使用extern关键字对动态库的函数进行声明: #in ...
- Linux 下Python调用C++编写的动态库
在工程中用到使用Python调用C++编写的动态库,结果报如下错误: OSError: ./extract_str.so: undefined symbol: _ZNSt8ios_base4InitD ...
- 如何在python中调用C语言代码
1.使用C扩展CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码 开发者有三种方法可以在自己的Python代码中来调用C编写的函数-ctypes,SWIG,Python/ ...
- java调用dll或so动态库文件(c++/c)
java调用dll或so动态库文件(c++/c) 博客分类: 工作 CC#C++JavaEclipse java调用dll或so动态库文件(c++/c)开发平台:Eclipse3.3.1.1+CD ...
- 【转载】cocos2dx 中 Android NDK 加载动态库的问题
原文地址:http://blog.csdn.net/sozell/article/details/10551309 cocos2dx 中 Android NDK 加载动态库的问题 闲聊 最近在接入各 ...
- 4.1 python中调用rust程序
概述 使用rust-cpython将rust程序做为python模块调用: 通常为了提高python的性能: 参考 https://github.com/dgrunwald/rust-cpython ...
- Django之在Python中调用Django环境
Django之在Python中调用Django环境 新建一个py文件,在其中写下如下代码: import os if __name__ == '__main__': os.environ.setdef ...
- Delphi调用C# 编写dll动态库
Delphi调用C# 编写dll动态库 编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目WZPayDll, using System.Runtime.InteropServices ...
随机推荐
- sql相关操作
1.两个不同数据库对应字段相应操作 //操作模版:insert into data2.table2(字段1,字段2,字段) select 字段j,字段k,字段m from data1.table1举例 ...
- checkbox,radio,selected相关操作
1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...
- pl sql练习(3)
1.s树形结构查询表中的数据:比如emp表中每个员工都有自己的头,即公司中的职位是按层次划分的,类似一个树,因此有时需要按层次显示查询的结果. select empno,mgr,ename,job f ...
- cocoapods导入第三方库
1.移除现有Ruby默认源 终端:gem sources --remove https://rubygems.org/ 2.使用新的源 终端:gem sources -a https://ruby.t ...
- 仅当使用了列的列表 并且 identity_insert 为 on 时 才能在表 中为标识列指定显式值
当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'products' 中的标识列插入显式值.” 示例: 1.首先建立一个有标识列的表:CREATE TABLE products (i ...
- Debian 使用杂记(一)
前几天又冲动的把系统换成Linux了,最开始接触Linux是2010年,那时候买了个本本,预装的是ubuntu8.10,自此知道除了windows原来还有其它操作系统. 不记得什么时候开始知道ubun ...
- Ubuntu安装tftp服务器
一.安装如下软件包: sudo apt-get install xinetd tftpd tftp 二.在/etc/xinetd.d/目录下创建tftp文件,并输入如下内容. 执行命令:sudo vi ...
- CDZSC_2015寒假新人(1)——基础 a
Description Contest time again! How excited it is to see balloons floating around. But to tell you a ...
- Servlet开发(一)
一.Servlet简介 Servlet是sun公司提供的用于开发动态web资源的技术.Sun公司在其API中提供了一个Servlet接口,用户若想开发一个动态web资源(即开发一个java程序向浏览器 ...
- 关于serialVersionUID的说明
1.为什么要使用serialVersionUID (1)对于实现了Serializable接口的类,可以将其序列化输出至磁盘文件中,同时会将其serialVersionUID输出到文件中. (2)然后 ...