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 ...
随机推荐
- 登陆权限验证Session和Cookie用法及BasePage类使用
最近在做ASP.NET的项目时,接触到了登陆权限模块,所有总结了一下登陆时用到的知识和方法技巧. 如图说明:实现的效果如图,由于验证码验证比较简单这里就不介绍了 首先用代码生成器生成项目,以三层为例进 ...
- c语言中的经典算法
c语言经典排序以及查找方法 冒泡排序 #include <stdio.h> int main() { int i, j, t, a[11]; /*定义变量及数组为基本整型*/ printf ...
- mapkit定位以及俯视视图
1.导入框架MapKit.framework,CoreGraphics.framework
- 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务 ...
- hdu3123GCC
Problem Description The GNU Compiler Collection (usually shortened to GCC) is a compiler system prod ...
- operator 类型转换符
参考脚本之家的这篇博客 http://www.jb51.net/article/41333.htm 类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义 ...
- xaml控件样式大全(太有用了)C#
地址:链接:http://pan.baidu.com/s/1jGlMyEi 密码:zaeg http://blog.csdn.net/lhx527099095/article/category/943 ...
- Oracle学习之常见错误整理
一.ORA-12154: TNS: 无法解析指定的连接标识符 在程序中连接Oracle数据库的方式与其他常用数据库,如:MySql,Sql Server不同,这些数据库可以通过直接指定IP的方式连接, ...
- python运维开发(十四)----HTML基本操作
内容目录: HTML概述 head标签 body中常用标签 css选择器 css常用属性 HTML HTML概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言) ...
- java学习一目了然——异常必知
java学习一目了然--异常必知 我们只要学java,异常肯定非常熟悉,该抛的时候抛一下就行.但是这其中还有点小细节需要注意.就用这个小短篇来说一下异常处理中的小细节吧. 异常处理 RuntimeEx ...