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

轉自:http://www.cnblogs.com/tevic/p/3645197.html html

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

系統

Linux 3.11.0-19-generic #33-Ubuntu x86_64 GNU/Linux

C++調用Python

Python模塊代碼:linux

#!/usr/bin/python #Filename:TestModule.py
def Hello(s): print ("Hello World") print(s) def Add(a, b): print('a=', a) print ('b=', b) return a + b class Test: def __init__(self): print("Init") def SayHello(self, name): print ("Hello,", name) return name

C++代碼ios

#include<iostream> #include<Python.h>
using namespace std; int main(int argc, char* argv[]) { //初始化python
 Py_Initialize(); //直接運行python代碼
    PyRun_SimpleString("print('----------Python Start')"); //引入當前路徑,不然下面模塊不能正常導入
    PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); //引入模塊
    PyRun_SimpleString("print('----------PyImport_ImportModule')"); PyObject *pModule = PyImport_ImportModule("TestModule"); //獲取模塊字典屬性
    PyRun_SimpleString("print('----------PyModule_GetDict')"); PyObject *pDict = PyModule_GetDict(pModule); //直接獲取模塊中的函數
    PyRun_SimpleString("print('----------PyObject_GetAttrString')"); PyObject *pFunc = PyObject_GetAttrString(pModule, "Hello"); //參數類型轉換,傳遞一個字符串。將c/c++類型的字符串轉換爲python類型,元組中的python類型查看python文檔
    PyRun_SimpleString("print('----------Py_BuildValue')"); PyObject *pArg = Py_BuildValue("(s)", "Hello Charity"); PyRun_SimpleString("print('----------PyEval_CallObject')"); //調用直接得到的函數,並傳遞參數
 PyEval_CallObject(pFunc, pArg); //從字典屬性中獲取函數
    PyRun_SimpleString("print('----------PyDict_GetItemString Add function')"); pFunc = PyDict_GetItemString(pDict, "Add"); //參數類型轉換,傳遞兩個整型參數
    pArg = Py_BuildValue("(i, i)", 1, 2); //調用函數,並獲得python類型的返回值
    PyObject *result = PyEval_CallObject(pFunc, pArg); //c用來保存c/c++類型的返回值
    int c; //將python類型的返回值轉換爲c/c++類型
    PyArg_Parse(result, "i", &c); //輸出返回值
    printf("a+b=%d\n", c); //經過字典屬性獲取模塊中的類
    PyRun_SimpleString("print('----------PyDict_GetItemString test class')"); PyObject *pClass = PyDict_GetItemString(pDict, "Test"); //實例化獲取的類
    PyRun_SimpleString("print('----------PyInstanceMethod_New test class')"); PyObject *pInstance = PyInstanceMethod_New(pClass); //調用類的方法
    PyRun_SimpleString("print('----------PyObject_CallMethod SayHello')"); result = PyObject_CallMethod(pInstance, "SayHello", "(Os)", pInstance, "Charity"); //輸出返回值
    char* name=NULL; PyRun_SimpleString("print('----------PyArg_Parse')"); PyArg_Parse(result, "s", &name); printf("%s\n", name); PyRun_SimpleString("print('Python End')"); //釋放python
 Py_Finalize(); getchar(); return 0; }

 

編譯:c++

g++ -I/usr/include/python3.5 pythonwithcpp.cpp -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -lpython3.5ubuntu

運行結果:app

root@ubuntu:/work/src/cython/cppcallpy# ./a.out
----------Python Start
----------PyImport_ImportModule
----------PyModule_GetDict
----------PyObject_GetAttrString
----------Py_BuildValue
----------PyEval_CallObject
Hello World
Hello Charity
----------PyDict_GetItemString Add function
a= 1
b= 2
a+b=3
----------PyDict_GetItemString test class
----------PyInstanceMethod_New test class
----------PyObject_CallMethod SayHello
Hello, Charity
----------PyArg_Parse
Charity
Python End函數

Python調用C++

C++代碼:post

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

編譯:學習

1 g++ -c -fPIC LibPythonTest.cpp
2 g++ -shared LibPythonTest.o -o LibPythonTest.so

Python代碼:

1 #!/bin/python
2 #Filename:PythonCallCpp.py
3 from ctypes import *
4 import os
5 libPythonTest = cdll.LoadLibrary('./LibPythonTest.so')
6 print libPythonTest.Add(1,1)

運行:

1 python PythonCallCpp.py

運行結果:

2
相關文章
相關標籤/搜索