How to embed Python into C ++

Source: Internet
Author: User

Python is a concise language in practical applications in computer languages. In practical applications, it can avoid inconvenience caused by many other languages, this article introduces how to embed C ++ into Python. If you understand this operation, you will get a better application in Python.

The following describes how to embed Python into C ++:

Python is a very good and elegant language. Without the troubles of semicolons and braces, you don't have to worry about braces matching and the trouble of skipping semicolons after the statement ends during development, this topic describes how to embed python into C ++ to bring your c ++ program to another level!
 

1. Install python,

2. First set the library file to include the include and lib in the python Directory into the library file and the library inclusion.

Now, let's start working. Let's look at a small program first:

 
 
  1. #include<python.h>#include<iostream>using
     namespace std;int main(){ Py_Initialize(); /* 

Initialize the python Interpreter

 
 
  1. */ PyRun_SimpleString("print 'hello world!'"); /*  

Call the python statement */Py_Finalize ();/* to end the python interpreter, release the resource */}, and output hello world under the console program! This is our first program. The first and third lines are required. Let's look at more code, this is to call the functions in the python script in the c ++ program: First, write a script:

 
 
  1. ___def helloworld():  
  2. _______print 'hello world!' 

Save it as the first. py file to the directory of the current project. The Code is as follows:

 
 
  1. #include<python.h>int main(){PyObject* p_module
     = NULL;PyObject* p_func = NULL; 
     Py_Initialize(); /*  

Python interpreter Initialization

 
 
  1. */ p_module = PyImport_ImportModule("first");
    p_func = PyObject_GetAttrString
    (p_module, "hello");PyEval_CallObject
    (p_func, NULL);Py_Finalize(); /*  

End the Python interpreter and release the resource.

 
 
  1. */ return 0;}  
  2. yImport_ImportModule()  

Is the file to import the script, the parameter is the file name

 
 
  1. PyObject_GetAttrString() 

Find the specified function PyEval_CallObject () from your import file. The NULL part is the function parameter. After running, you will see hello world! String.

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.