If you want to embed Python in C + +, you need to add Python's include file directory and Lib file directory to the VC. Let's take a look at how to embed Python in C + +.
VC6.0, open tools->options->directories->show directories for, add the Inlude directory under the Python installation directory to the Inlude files item, Add the Libs directory to the library files item.
VC2005, open the Tools->options-> project and Solution->vc++ directory, and do the same work.
The code is as follows:
Error in debug execution, "Unable to find Python31_d.lib file", after the reason is: in debug generated must have Python31_d.lib file, otherwise can only be generated under release
#include
int main () { py_initialize (); Pyrun_simplestring ("Print ' Hi, python! '"); Py_finalize (); return 0; }
Py_initialize function prototype is: void Py_initialize ()
This function must be used when Python is embedded in C + + +, which initializes the Python interpreter and must be called before other PYTHON/C APIs can be used. You can use the Py_isinitialized function to determine if the initialization succeeded and return true successfully.
The Pyrun_simplestring function prototype is an int pyrun_simplestring (const char *command) used to execute a python code.
Note: Is it necessary to maintain indentation between statements?
The Py_finalize function prototype is void Py_finalize (), which closes the Python interpreter and frees up the resources that the interpreter occupies.
The Pyrun_simplefile function can be used to run the ". Py" script file, the function prototype is as follows:
int Pyrun_simplefile (FILE *fp, const char *filename);
Where FP is an open file pointer, filename is the Python script file name to run. However, because the function is officially published by Visual Studio 2003.NET, if you use a different version of the compiler, the file definition may cause a crash due to version reasons. Also, for brevity, you can replace the function with the following:
Pyrun_simplestring ("execfile (' file.py ')"); Using execfile to run Python files
Py_buildvalue () is used to convert numbers and strings into the corresponding data types in Python (all Python types are declared as Pyobject types in the C language), and the function prototypes are as follows:
Pyobject *py_buildvalue (const char *format, ...);
Pystring_string () is used to convert a variable of type pyobject* into a char* type that can be processed by the C language, with the following prototype:
char* pystring_string (Pyobject *p);
The above is an introduction to how Python is embedded in C/s + + content