Python Embedded C ++ (1) -- helloworld
When I came to csdn, I Was Just familiar with python. At that time, I was very interested in the python embedding part, but I had no time to figure out its veil. Therefore, I have never used the embedding function, another reason is that I have not actually used python to write something officially useful. However, looking back at this part, I found it quite simple. I used to translate this part, but I didn't have the energy because of the time. So here I will describe every Python/C embedded function I have explored using examples, I want to see the code is easier to understand than I have explained.
Before helloworld, you have to mention the embedded configuration.
Steps:
1: Go to the official website to download the python installation program and install Python (I think this step is a bit redundant, but write it here)
2: You may need to download the python (same as the installed version) source code, because we need to compile the Link under debug to generate the corresponding debug library, if you can find someone else's compiled library on the Internet, you can also use someone else's library directly. In this way, two DEBUG Versions of. lib and. dll are obtained.
3: Move the files in the include folder under the python directory to include in the compilation system, and then release and DEBUG Versions. lib and. DLL to the specified folder (lib to the compiling system Lib, DLL to Windows/system32 /)
4: OK. The configuration is complete.
Let's look at the Code:
# Include "Python/python. H" // I put the python header files in my own folder named python.
Int main (INT argc, char ** argv)
{
Py_initialize (); // This function starts the python interpreter, Which is initialization.
////////////////////////
// Run commands by loading files. file is a structure in the stdio. h file of the C standard library and should be familiar with the C language (but I used it for the first time, Khan ...)
File * fr;
Fopen_s (& FR, "test.txt", "R ");
Pyrun_simplefile (FR, "test.txt ");
Fclose (FR );
// Run the command directly. The command is a string. Note that parentheses must be added to Python 3.1.1.
Pyrun_simplestring ("Print ('Hello world, I am python! ')");
///////////////////////
Py_finalize (); // close the python interpreter.
Return 0;
}
// Comes with test.txt
/*
From time import time, ctime
Print ('Today is ', ctime (Time ()))
*/
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/xiadasong007/archive/2009/09/02/4511841.aspx
Python Embedded C ++ (2) -- import module
First, I have to mention a very good website: http://www.codase.com/index.html
This is what I found when searching for Embedded functions. There are a lot of small instance code in it, so it is really time to see (haha)
The preceding describes the configurations needed for embedding (see the http://blog.csdn.net/xiadasong007/archive/2009/09/02/4511841.aspx), this time mainly to illustrate how to call the basic methods of functions in the python module in C/C ++.
Prerequisites:
1: pyobject * obj is a bit like a handle in the SDK.
2: Call the python module process: first load the module. After loading, you can directly access it (⊙ o ⊙ ~! Very simple ~
Okay. Let's see the code:
# Include "Python/python. H"
# Include <iostream>
Using namespace STD;
Int main (INT argc, char ** argv)
{
Pyobject * pmodule, * pfunc, * pvalue;
Py_initialize ();
////////////////
Pmodule = pyimport_importmodule ("mat"); // mat is a small module (mat. py) Written in Python by myself. For details, see the appendix below.
Pvalue = pyobject_callmethod (pmodule, "add", "II",); // directly access the function add (A, B) in the module through the module obtained above)
// Note that the "II" here is actually equivalent to the "% d" feature in C. Please refer to the documentation
Cout <pylong_aslong (pvalue) <Endl; // convert the obtained pvalue value to the long type in C.
Pfunc = pyobject_getattrstring (pmodule, "add"); // you can use this function to obtain the function object.
Pvalue = pyobject_callfunction (pfunc, "II",); // execute a function through a function object
Cout <pylong_aslong (pvalue) <Endl;
////////////////
Py_finalize ();
Return 0;
}
// Appendix: mat. py
/*
Def add (A, B ):
Return A + B
*/
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/xiadasong007/archive/2009/09/02/4512797.aspx
Python Embedded C ++ (3) -- import class
Next to the previous article import module (http://blog.csdn.net/xiadasong007/archive/2009/09/02/4512797.aspx), continue to analyze the basics of embedding. I will not talk about it this time. If you have any questions, please remember to check more English documents. I have little knowledge in this field in China.
Let's take a look at the code and I will go to bed after writing it ~
# Include "Python/python. H"
# Include <iostream>
Using namespace STD;
Int main (INT argc, char ** argv)
{
Pyobject * pmodule, * Pclass, * pstuobj, * pfunc;
Py_initialize ();
////////////////
// Load module Stu. py
Pmodule = pyimport_importmodule ("Stu ");
// Obtain the class student from the module
Pclass = pyobject_getattrstring (pmodule, "student ");
// Generate a student object Jack. Note that parentheses (s) must be added to version 3.1; otherwise, an error will occur.
// Pyobject_callobject is similar to another commonly used pyeval_callobject. For details, see the appendix.
Pyobject * temp = py_buildvalue ("(s)", "Jack ");
Pstuobj = pyobject_callobject (Pclass, temp );
// Obtain the member function printname in the function object Jack,
// This getattr means to get the attributes of the current object (not limited to attributes in C ++)
Pfunc = pyobject_getattrstring (pstuobj, "printname ");
// Call the printname Function
Pyeval_callobject (pfunc, null );
////////////////
Py_finalize ();
Return 0;
}
/*
Appendix:
1: Student class. I found that parentheses must be added after print. Otherwise, there will be problems.
Class student:
Name = ""
Def _ init _ (self, name ):
Self. Name = Name
Def printname (Self ):
Print (self. Name)
2: differences between pyobject_callobject and pyeval_callobject:
It can be seen that the latter is more direct and accepts null, and does explicit type checks
For ARGs and kwds.
Pyobject_callobject (pyobject * o, pyobject *)
{
! Pyobject * R;
! Pyobject * ARGs =;
!
! If (ARGs = NULL ){
! ARGs = pytuple_new (0 );
! If (ARGs = NULL)
! Return NULL;
! }
!
! R = pyeval_callobject (O, argS );
!
! If (ARGs! = ){
! Py_decref (ARGs );
! }
!
! Return R;
}
Pyobject_callobject (pyobject * o, pyobject *)
{
! Return pyeval_callobjectwithkeywords (O, A, null );
}
*/
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/xiadasong007/archive/2009/09/03/4513615.aspx