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 seeCodeIt is easier to understand than I can explain.
Before helloworld, you have to mention the embedded configuration.
Steps:
1: Go to the official website to download Python InstallationProgramInstall Python (I think this step is a bit redundant, but write it)
2: You may need to download Python (same as the installation version)Source codeBecause we need to compile the Link under debug to generate the corresponding debug library. If you can find the compiled library on the Internet, you can also use it 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 ()))
*/