Introduction to Python Embedded C/C ++-related practical application code

Source: Internet
Author: User

If you want to embed Python into C/C ++, you need to add the include file directory and lib file directory of Python in VC. The following describes how to embed Python into the specific solution of C/C ++, I hope you will learn a lot from it.

In VC6.0, choose tools> options> directories> show directories for, and add the inlude directory under the Python installation directory to the inlude files item, add the libs directory to the library files item.

Under VC2005, open the tools-> options-> project and solution-> VC ++ directory, and then do the same work.

The Code is as follows:

An error occurred while executing the command in debug. "The python31_d.lib file cannot be found." The cause is that the python31_d.lib file must be generated in debug; otherwise, the file can only be generated in release.

 
 
  1. #include <python.h> 
  2. int main()  
  3. {  
  4. Py_Initialize();  
  5. PyRun_SimpleString("Print 'hi, python!'");  
  6. Py_Finalize();  
  7. return 0;  

The prototype of the Py_Initialize function is void Py_Initialize ()

You must use this function when embedding Python in C/C ++. It initializes the Python interpreter and must call this function before using other Python/C APIs. You can use the Py_IsInitialized function to determine whether the initialization is successful. If the initialization is successful, True is returned.
The prototype of the PyRun_SimpleString function is int PyRun_SimpleString (const char * command), which is used to execute a piece of Python code.

Note: Do you need to maintain the indentation between statements?

The prototype of the Py_Finalize function is void Py_Finalize (). It is used to close the Python interpreter and release the resources occupied by the interpreter.

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 );

Fp is the open file pointer, and filename is the python script file name to be run. However, this function is officially released by visual studio 2003. NET. If you use a compiler of another version, the FILE definition may crash due to version reasons. In addition, you can use the following method to replace this function for convenience:

PyRun_SimpleString ("execfile ('file. py')"); // execfile is used to run python files.

  • Convert a Python script to an executable program in windows
  • Frequently Used functions when writing shell scripts in Python
  • Introduction to commonly used files in shell scripts written in Python
  • Python scripts are often used for gdb debugging.
  • Call relationships between functions in a Python script

Py_BuildValue () is used to convert numbers and strings into corresponding data types in Python. In C, all Python types are declared as PyObject types. The function prototype is as follows:

PyObject * Py_BuildValue (const char * format ,.....);

PyString_String () is used to convert a variable of the PyObject * type into a char * type that can be processed by C language. The prototype is as follows:

Char * PyString_String (PyObject * p );

The above is an introduction to how to embed Python into C/C ++. I hope you will gain some benefits.
 

Related Article

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.