[轉]Linux下Python與C++混合編程

來源:互聯網
上載者:User

標籤:self   final   include   技術   pac   輸出   sim   load   with   

最近在做一個CUDA的項目,記錄下學習心得.

系統
Linux 3.11.0-19-generic #33-Ubuntu x86_64 GNU/Linux
C++調用Python

Python模組代碼:

 1 #!/usr/bin/python 2 #Filename:TestModule.py 3 def Hello(s): 4     print "Hello World" 5     print s 6  7 def Add(a, b): 8     print ‘a=‘, a 9     print ‘b=‘, b10     return a + b11 12 class Test:13     def __init__(self):14         print "Init"15     def SayHello(self, name):16         print "Hello,", name17         return name

C++代碼:

 1 #include<iostream> 2 #include<Python.h> 3 using namespace std; 4 int main(int argc, char* argv[]) 5 { 6     //初始化python 7     Py_Initialize(); 8  9     //直接運行python代碼10     PyRun_SimpleString("print ‘Python Start‘");11 12     //引入當前路徑,否則下面模組不能正常匯入13     PyRun_SimpleString("import sys");  14     PyRun_SimpleString("sys.path.append(‘./‘)");  15 16     //引入模組17     PyObject *pModule = PyImport_ImportModule("TestModule");18     //擷取模組字典屬性19     PyObject *pDict = PyModule_GetDict(pModule);20 21     //直接擷取模組中的函數22     PyObject *pFunc = PyObject_GetAttrString(pModule, "Hello");23 24     //參數類型轉換,傳遞一個字串。將c/c++類型的字串轉換為python類型,元組中的python類型查看python文檔25     PyObject *pArg = Py_BuildValue("(s)", "Hello Charity");26 27     //調用直接獲得的函數,並傳遞參數28     PyEval_CallObject(pFunc, pArg);29 30     //從字典屬性中擷取函數31     pFunc = PyDict_GetItemString(pDict, "Add");32     //參數類型轉換,傳遞兩個整型參數33     pArg = Py_BuildValue("(i, i)", 1, 2);34 35     //調用函數,並得到python類型的傳回值36     PyObject *result = PyEval_CallObject(pFunc, pArg);37     //c用來儲存c/c++類型的傳回值38     int c;39     //將python類型的傳回值轉換為c/c++類型40     PyArg_Parse(result, "i", &c);41     //輸出傳回值42     printf("a+b=%d\n", c);43 44     //通過字典屬性擷取模組中的類45     PyObject *pClass = PyDict_GetItemString(pDict, "Test");46 47     //執行個體化擷取的類48     PyObject *pInstance = PyInstance_New(pClass, NULL, NULL);49     //調用類的方法50     result = PyObject_CallMethod(pInstance, "SayHello", "(s)", "Charity");51     //輸出傳回值52     char* name=NULL;53     PyArg_Parse(result, "s", &name);54     printf("%s\n", name);55 56     PyRun_SimpleString("print ‘Python End‘");57 58     //釋放python59     Py_Finalize();60     getchar();61     return 0;62 }

編譯:

g++ -I/usr/include/python2.7 PythonWithCpp.cpp -lpython2.7

運行結果:

Python StartHello WorldHello Charitya= 1b= 2a+b=3InitHello, CharityCharityPython End
Python調用C++

C++代碼:

1 //用C++必須在函數前加extern "C"2 extern "C" int Add(int a,int b)3 {4     return a+b;5 }

編譯:

1 g++ -c -fPIC LibPythonTest.cpp2 g++ -shared LibPythonTest.o -o LibPythonTest.so

Python代碼:

1 #!/bin/python2 #Filename:PythonCallCpp.py3 from ctypes import *4 import os5 libPythonTest = cdll.LoadLibrary(‘./LibPythonTest.so‘)6 print libPythonTest.Add(1,1)

運行:

1 python PythonCallCpp.py

運行結果:

2

[轉]Linux下Python與C++混合編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.