Go Hybrid programming for Python and C + + under Linux

Source: Internet
Author: User

Recently doing a Cuda project, documenting the learning experience.

System
Linux 3.11.0-19-generic #33-ubuntu x86_64 gnu/linux
C + + calls Python

Python Module code:

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 class test:13     def __init__ (self):         print "Init "     def SayHello (self, name):         print" Hello, ", Name17         return name

C + + code:

 1 #include <iostream> 2 #include <Python.h> 3 using namespace std; 4 int main (int argc, char* argv[]) 5 {6//Initialize Python 7 py_initialize (); 8 9//Run Python code directly pyrun_simple  String ("print ' Python Start '); 11 12//Introduce the current path, otherwise the module below cannot be imported into pyrun_simplestring (" Import sys ");  Pyrun_simplestring ("Sys.path.append ('./')"); 15 16//Introduction module Pyobject *pmodule = Pyimport_importmodule ("Testmodule"); 18//Get module dictionary properties pyobject *pdict = P     Ymodule_getdict (Pmodule); 20 21//direct access to functions in module pyobject *pfunc = pyobject_getattrstring (Pmodule, "Hello"); 23 24 Parameter type conversion, passing a string. Convert a C + + type string to a Python type, the Python type in the tuple view python document Pyobject *parg = Py_buildvalue ("(s)", "Hello Charity"); 26 27/Call Direct access to the function and pass parameters to Pyeval_callobject (PFunc, PARG); 29 30//Get function from dictionary attribute = PFunc = pydict_getitemstring (pdict, "ADD") ; 32//parameter type conversion, pass two integer parameter parg = Py_buildvalue ("(I, I)", 1, 2), 34 35//Call function, and get the return value of Python type Pyobject *resu lt = pyeval_calLOBject (PFunc, PARG); The PNs//c is used to hold the return value of the C + + type. int c;39//Convert the return value of the Python type to C + + type-pyarg_parse (result, "I", &AMP;C); 41//Output return value in printf ("a+b=%d\n", c); 43 44//Dictionary property gets the class in the module Pyobject *pclass = Pydict_getitemstrin     G (Pdict, "Test"); 46 47//instantiation Gets the class Pyobject *pinstance = Pyinstance_new (pClass, NULL, NULL); 49//method of calling Class 50 result = Pyobject_callmethod (pinstance, "SayHello", "(s)", "Charity"); 51//Output return value. char* name=null;53 PYARG_PA RSE (result, "s", &name); printf ("%s\n", name), pyrun_simplestring ("print ' Python End '); 57 58//Release P Ython59 py_finalize (); GetChar (); return 0;62}

Compile:

g++-i/usr/include/python2.7 pythonwithcpp.cpp-lpython2.7

Operation Result:

Python Starthello Worldhello charitya= 1b= 2a+b=3inithello, Charitycharitypython End
Python calls C + +

C + + code:

1//In C + + must be in front of function with extern "C" 2 extern "C" int Add (int a,int b) 3 {4     return a+b;5}

Compile:

1 g++-c-fpic libpythontest.cpp2 g++-shared libpythontest.o-o libpythontest.so

Python code:

1 #!/bin/python2 #Filename:P ythoncallcpp.py3 from ctypes import os5 libpythontest = Cdll. LoadLibrary ('./libpythontest.so ') 6 print libpythontest.add (+)

Run:

1 python pythoncallcpp.py

Operation Result:

2

[]linux Python and C + + mixed programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.