Python与C/C++ 模块相互调用

Python调用C动态链接库
Python调用C库很简单,不经过任何封装打包成so,再使用python的ctypes调用即可。
<test.cpp 生成动态库的源文件>

  1. #include <stdio.h> 
  2. extern “C” {
  3.         void display() {
  4.                 printf(“This is Display Function\n”);
  5.         }
  6. }
  7. g++ test.cpp -fPIC -shared -o libtest.so

<call.py 调用动态库的源文件>

  1. import ctypes
  2. so = ctypes.CDLL(“./libtest.so”)
  3. so.display()

 

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

Python调用C++(含类,重载)动态链接库
但是调用C++的so就有点麻烦了,网上找了下,大部分都是需要extern “C” 来辅助,也就是说还是只能调用C函数 不能直接调用方法 但是能解析C++方法。
<test.cpp 生成动态库的源文件>

  1. #include <Akita/Akita.h> 
  2. class TestLib{
  3.         public:
  4.                 void display();
  5.                 void display(int a);
  6. };
  7. void TestLib::display() {
  8.         cout<<“First display”<<endl;
  9. }
  10. void TestLib::display(int a) {
  11.         cout<<“Second display”<<endl;
  12. }
  13. extern “C” {
  14.         TestLib obj;
  15.         void display() {
  16.                obj.display();
  17.         }
  18.         void display_int() {
  19.                obj.display(2);
  20.         }
  21. }

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

<call.py 调用动态库的源文件>

  1. import ctypes
  2. so = ctypes.CDLL(“./libtest.so”)
  3. so.display()
  4. so.display_int(1)

运行结果如下:

  1. ^[root@:~/Projects/nugget/kvDB-py]#python call.py
  2. First display
  3. Second display

C/C++调用Python模块

 

<test.cpp >

  1. #include <Akita/Akita.h> 
  2. #include <Python.h> 
  3. int main() {
  4.         Py_Initialize();
  5.         if (!Py_IsInitialized())  return FALSE;
  6.         PyRun_SimpleString(“import sys”);
  7.         PyRun_SimpleString(“sys.path.append(‘./’)”);
  8.         //import Module 
  9.         PyObject* pModule = PyImport_ImportModule(“hello”);
  10.         if (!pModule) {
  11.                 cout<<“Can’t import Module!/n”<<endl;
  12.                 return -1;
  13.         }
  14.         PyObject* pDict = PyModule_GetDict(pModule);
  15.         if (!pDict) {
  16.                 return -1;
  17.         }
  18.         //fetch Function 
  19.         PyObject* pFunHi = PyDict_GetItemString(pDict, “display”);
  20.         PyObject_CallFunction(pFunHi, “s”“Crazybaby”);
  21.         Py_DECREF(pFunHi);
  22.         //Release 
  23.         Py_DECREF(pModule);
  24.         Py_Finalize();
  25.         return 0;
  26. }

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

 
<call.py>

  1. def display(name):
  2.         print “hi”,name

———

C++为Python编写扩展模块
Python为C++提供脚本接口。

有了两者交互 方便之极。

 

from:blog.csdn.net/crazyjixiang  作者:crazyjixiang

没有评论

  • (Required)
  • (Required, will not be published)

注意: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。使用'@all ',将会将评论发送给之前所有其它评论者。请务必注意user必须和评论者名相匹配(大小写一致)。
  • 关于BLOG

    本站架设在MyHost123上,由CentOS强力驱动

  • 分类目录

  • 那些句子

    我无意成为一个高尚道德的说教者,只是在本书阅读过程中,逐渐认到:会编写优秀的代码,会设计优秀的架构,有敢于担当的社会责任心,是一件多么令人骄傲和让人尊敬的事情。《代码之美 序》
  • 近期文章

  • 标签云

  • 我的饭否

    饭否
  • 赞助商链接

  • 已屏蔽的傻逼评论