python调用c动态库函数_Python调用C动态链接库

本文详细介绍了如何使用Python的ctypes模块调用C动态链接库,包括直接调用C函数和通过extern "C"调用C++方法。同时,也展示了C/C++调用Python模块的方法,提供了完整的源码示例。
摘要由CSDN通过智能技术生成

Python调用C动态链接库Python调用C库很简单,不经过任何封装打包成so,再使用python的ctypes调用即可。

#include 

extern"C"{

voiddisplay() {

printf("This is Display Function\n");

}

}

g++ test.cpp -fPIC -shared -o libtest.so

importctypes

so = ctypes.CDLL("./libtest.so")

so.display()

这里需要注意的是:使用g++编译生成动态库的代码中的函数 或者 方法时, 需要 使用extern "C"来进行编译Python调用C++(含类,重载)动态链接库但是调用C++的so就有点麻烦了,网上找了下,大部分都是需要extern "C" 来辅助,也就是说还是只能调用C函数 不能直接调用方法 但是能解析C++方法。

#include 

classTestLib{

public:

voiddisplay();

voiddisplay(inta);

};

voidTestLib::display() {

cout<

}

voidTestLib::display(inta) {

cout<

}

extern"C"{

TestLib obj;

voiddisplay() {

obj.display();

}

voiddisplay_int() {

obj.display(2);

}

}g++ test.cpp -fPIC -shared -o libtest.so使用这种方法有点麻烦 但是可以解决问题。注意到后面还是会有个extern "C" 不然构建后的动态链接库没有这些函数的符号表的。

importctypes

so = ctypes.CDLL("./libtest.so")

so.display()

so.display_int(1)运行结果如下:

^[root@:~/Projects/nugget/kvDB-py]#python call.py

First display

Second displayC/C++调用Python模块

Python调用C动态链接库

Python调用C库很简单,不经过任何封装打包成so,再使用python的ctypes调用即可。

源码打印?

#include

extern "C" {

void display() {

printf("This is Display Function\n");

}

}

g++ test.cpp -fPIC -shared -o libtest.so

源码打印?

import ctypes

so = ctypes.CDLL("./libtest.so")

so.display()

这里需要注意的是:使用g++编译生成动态库的代码中的函数 或者 方法时, 需要 使用extern "C"来进行编译

Python调用C++(含类,重载)动态链接库

但是调用C++的so就有点麻烦了,网上找了下,大部分都是需要extern "C" 来辅助,也就是说还是只能调用C函数 不能直接调用方法 但是能解析C++方法。

源码打印?

#include

class TestLib{

public:

void display();

void display(int a);

};

void TestLib::display() {

cout<

}

void TestLib::display(int a) {

cout<

}

extern "C" {

TestLib obj;

void display() {

obj.display();

}

void display_int() {

obj.display(2);

}

}

g++ test.cpp -fPIC -shared -o libtest.so

使用这种方法有点麻烦 但是可以解决问题。注意到后面还是会有个extern "C" 不然构建后的动态链接库没有这些函数的符号表的。

源码打印?

import ctypes

so = ctypes.CDLL("./libtest.so")

so.display()

so.display_int(1)

运行结果如下:

源码打印?

^[root@:~/Projects/nugget/kvDB-py]#python call.py

First display

Second display

C/C++调用Python模块

源码打印?

#include

#include

int main() {

Py_Initialize();

if (!Py_IsInitialized())  return FALSE;

PyRun_SimpleString("import sys");

PyRun_SimpleString("sys.path.append('./')");

//import Module

PyObject* pModule = PyImport_ImportModule("hello");

if (!pModule) {

cout<

return -1;

}

PyObject* pDict = PyModule_GetDict(pModule);

if (!pDict) {

return -1;

}

//fetch Function

PyObject* pFunHi = PyDict_GetItemString(pDict, "display");

PyObject_CallFunction(pFunHi, "s", "Crazybaby");

Py_DECREF(pFunHi);

//Release

Py_DECREF(pModule);

Py_Finalize();

return 0;

}

#g++ test.cpp -I/usr/local/include/python2.7 -ldl -lutil -lpthread -lpython2.7

源码打印?

def display(name):

print "hi",name

---------

C++为Python编写扩展模块

Python为C++提供脚本接口。

有了两者交互 方便之极。

原文链接:http://blog.csdn.net/crazyjixiang/article/details/6773382源码打印?#include 

#include 

intmain() {

Py_Initialize();

if(!Py_IsInitialized())returnFALSE;

PyRun_SimpleString("import sys");

PyRun_SimpleString("sys.path.append('./')");

//import Module

PyObject* pModule = PyImport_ImportModule("hello");

if(!pModule) {

cout<

return-1;

}

PyObject* pDict = PyModule_GetDict(pModule);

if(!pDict) {

return-1;

}

//fetch Function

PyObject* pFunHi = PyDict_GetItemString(pDict, "display");

PyObject_CallFunction(pFunHi, "s","Crazybaby");

Py_DECREF(pFunHi);

//Release

Py_DECREF(pModule);

Py_Finalize();

return0;

}#g++ test.cpp -I/usr/local/include/python2.7 -ldl -lutil -lpthread -lpython2.7

defdisplay(name):

print"hi",name

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值