一、环境: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++写的动态库的更多相关文章

  1. 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 ...

  2. 使用ctypes在Python中调用C++动态库

    使用ctypes在Python中调用C++动态库 入门操作 使用ctypes库可以直接调用C语言编写的动态库,而如果是调用C++编写的动态库,需要使用extern关键字对动态库的函数进行声明: #in ...

  3. Linux 下Python调用C++编写的动态库

    在工程中用到使用Python调用C++编写的动态库,结果报如下错误: OSError: ./extract_str.so: undefined symbol: _ZNSt8ios_base4InitD ...

  4. 如何在python中调用C语言代码

    1.使用C扩展CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码 开发者有三种方法可以在自己的Python代码中来调用C编写的函数-ctypes,SWIG,Python/ ...

  5. java调用dll或so动态库文件(c++/c)

    java调用dll或so动态库文件(c++/c) 博客分类:  工作 CC#C++JavaEclipse  java调用dll或so动态库文件(c++/c)开发平台:Eclipse3.3.1.1+CD ...

  6. 【转载】cocos2dx 中 Android NDK 加载动态库的问题

     原文地址:http://blog.csdn.net/sozell/article/details/10551309 cocos2dx 中 Android NDK 加载动态库的问题 闲聊 最近在接入各 ...

  7. 4.1 python中调用rust程序

    概述 使用rust-cpython将rust程序做为python模块调用: 通常为了提高python的性能: 参考 https://github.com/dgrunwald/rust-cpython ...

  8. Django之在Python中调用Django环境

    Django之在Python中调用Django环境 新建一个py文件,在其中写下如下代码: import os if __name__ == '__main__': os.environ.setdef ...

  9. Delphi调用C# 编写dll动态库

    Delphi调用C# 编写dll动态库 编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目WZPayDll, using System.Runtime.InteropServices ...

随机推荐

  1. C# 单例模式(转)

    C#设计模式学习笔记-单例模式 最近在学设计模式,学到创建型模式的时候,碰到单例模式(或叫单件模式),现在整理一下笔记. 在<Design Patterns:Elements of Resuab ...

  2. ios9基础知识(UI)总结

    UIWindow.UILabel.UIColor.UIScreen.UIViewController.UIView.UIControl.UIButton.IBOutlet.IBAction.UISte ...

  3. C++程序设计实践指导1.7超长数列中n个数排序改写要求实现

    改写要求1:将以上程序改写为适合超长整数 改写要求2:将以上程序改写为适合超长数列 改写要求3:将数列中指定位置m开始的n个结点重新按降序排序 改写要求4:输出指定位置m开始的n个结点的超长整数 #i ...

  4. linux学习笔记之线程同步机制

    一.基础知识. 1:线程同步机制:互斥量,读写锁,条件变量,自旋锁,屏障. 1,互斥量:每个进程访问被互斥量保护的资源时,都需要先对互斥量进行判断. 1)互斥量重要属性:进程共享属性,健壮属性,类型属 ...

  5. silverlight中鼠标放在对象的提示事件

    1.xaml 中实现 <Rectangle x:Name="toolTip" Grid.Column="0" Grid.Row="1" ...

  6. Dubbo使用详解及环境搭建

    一:Dubbo简介 Dubbo是阿里巴巴提供的开源的SOA(面向服务的体系结构)服务化治理的技术框架,据说只是一部分开源的,但一些基本的需求已经可以满足的,而且可扩展性.是一种能取代PHRPC的服务调 ...

  7. jquery 获取自定义属性(attr 和 prop的区别)

    在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...

  8. php递归函数,性能给力

    function arPro($data,$res=array(),$pid='0',$level='0'){ foreach ($data as $k => $v){ if($v['comme ...

  9. python核心编程-习题-第二章

    PS:PDF在线地址:http://bcmi.sjtu.edu.cn/~zhaohai/ptm2012/data/Python-kernel.programming.v2.pdf 2-1  变量,pr ...

  10. Mongo客户端

    http://www.linuxidc.com/Linux/2012-07/64233.htm http://www.oschina.net/p/rockmongo http://www.cnblog ...