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 ...
随机推荐
- 【初级坑跳跳跳】第一个应用布局学习的代码运行时出错(manifest里未将activity先注册,控件错误)
首先,根据书中想要实现的结果,看了下书中代码,大致知道布局是怎么样的,然后根据图片自己写xml, 1.运行时第一个坑是 忘记在AndroidManifest.xml里先注册activity,导致运行时 ...
- ios 项目被拒绝各种理由
. Terms and conditions(法律与条款) 1.1 As a developer of applications for the App Store you are bound by ...
- SQL中Truncate的用法(转)
转自:http://www.studyofnet.com/news/555.html 本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE TABLE用于删除表中的所 ...
- STM32的例程GPIO的汇编指令初探
任务一:寻找main函数的汇编指令集 任务二:寻找main函数中的SystemClock_Config函数的汇编指令集 寻找main函数的汇编指令集 运行例程中GPIO工程时,总会加载startup_ ...
- mybatis使用order by注意
直接用动态参数生成,不会排序: <if test="orderColumn!=null and orderColumn != ''"> ORDER BY #{order ...
- 安装windows7和ubuntu双系统后引导项设置
win7系统,U盘安装ubuntu,在选择[安装启动引导器的设备]时,1.如果你选择的是/dev/sda,即整个硬盘,他会将启动引导器使用grub进行系统引导,而不再使用windows loader, ...
- Oracle ORA-01555(快照过旧)
一.引言: [oracle@yft yft]$ oerr ora 01555 01555, 00000, "snapshot too old: rollback segment number ...
- 新建Android项目的时候,选择SDK的区别
选择新建一个Android项目时候,出现MiniMum Required SDK.Target SDK.Compile With.Theme,如下所示,分别是什么意思呢? MinMum Require ...
- Inno Setup 网页显示插件 webctrl (V2.1 版本)
原文 http://restools.hanzify.org/article.asp?id=90 Inno Setup网页显示插件 webctrl能够显示所有 IE 中能够显示的东西. 引用内容 ; ...
- adobe reader安装完成之前被中断,错误代码150210解决方法
adobe reader安装完成之前被中断,错误代码150210解决方法出现这种情况是因为之前安装过adobe reader但是没有卸载删除干净进而导致重新安装时无法安装.为什么卸载不了大多数是因为3 ...