分享

Python调用C动态链接库

 闲闲流水 2013-08-16
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.   
  8. };  
  9. void TestLib::display() {  
  10.         cout<<"First display"<<endl;  
  11. }  
  12.   
  13.   
  14. void TestLib::display(int a) {  
  15.         cout<<"Second display"<<endl;  
  16. }  
  17. extern "C" {  
  18.         TestLib obj;  
  19.         void display() {  
  20.                obj.display();   
  21.         }  
  22.         void display_int() {  
  23.                obj.display(2);   
  24.         }  
  25. }  
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 >

Python调用C动态链接库 
Python调用C库很简单,不经过任何封装打包成so,再使用python的ctypes调用即可。 
<test.cpp 生成动态库的源文件> 
源码打印?
#include <stdio.h>  
extern "C" {  
        void display() {  
                printf("This is Display Function\n");   
        }  
}  
g++ test.cpp -fPIC -shared -o libtest.so  
<call.py 调用动态库的源文件>

源码打印?
import ctypes  
so = ctypes.CDLL("./libtest.so")  
so.display()  
这里需要注意的是:使用g++编译生成动态库的代码中的函数 或者 方法时, 需要 使用extern "C"来进行编译 

Python调用C++(含类,重载)动态链接库 
但是调用C++的so就有点麻烦了,网上找了下,大部分都是需要extern "C" 来辅助,也就是说还是只能调用C函数 不能直接调用方法 但是能解析C++方法。 
<test.cpp 生成动态库的源文件> 
源码打印?
#include <Akita/Akita.h>  
class TestLib{  
        public:  
                void display();  
                void display(int a);  
  
  
};  
void TestLib::display() {  
        cout<<"First display"<<endl;  
}  
  
  
void TestLib::display(int a) {  
        cout<<"Second display"<<endl;  
}  
extern "C" {  
        TestLib obj;  
        void display() {  
               obj.display();   
        }  
        void display_int() {  
               obj.display(2);   
        }  
}  
g++ test.cpp -fPIC -shared -o libtest.so 
使用这种方法有点麻烦 但是可以解决问题。注意到后面还是会有个extern "C" 不然构建后的动态链接库没有这些函数的符号表的。 
<call.py 调用动态库的源文件>

源码打印?
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模块
<test.cpp >

源码打印?
#include <Akita/Akita.h>  
#include <Python.h>  
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<<"Can't import Module!/n"<<endl;    
                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

<call.py>
源码打印?
def display(name):  
        print "hi",name  

---------

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

有了两者交互 方便之极。




原文链接:http://blog.csdn.net/crazyjixiang/article/details/6773382源码打印
  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.   
  9.         //import Module  
  10.         PyObject* pModule = PyImport_ImportModule("hello");    
  11.         if (!pModule) {    
  12.                 cout<<"Can't import Module!/n"<<endl;    
  13.                 return -1;    
  14.         }    
  15.   
  16.         PyObject* pDict = PyModule_GetDict(pModule);    
  17.         if (!pDict) {    
  18.                 return -1;    
  19.         }    
  20.   
  21.         //fetch Function  
  22.         PyObject* pFunHi = PyDict_GetItemString(pDict, "display");    
  23.         PyObject_CallFunction(pFunHi, "s""Crazybaby");    
  24.         Py_DECREF(pFunHi);    
  25.   
  26.         //Release  
  27.         Py_DECREF(pModule);    
  28.         Py_Finalize();    
  29.         return 0;    
  30. }  
#g++ test.cpp -I/usr/local/include/python2.7 -ldl -lutil -lpthread -lpython2.7


<call.py>

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多