Call the python script in C, C ++ code

Source: Internet
Author: User

At the opening meeting today, it was mentioned that other groups would need to build a general task system, which involves C ++'s logic to call scripts developed in other languages. Previously, I heard from colleagues from the entertainment industry that they have embedded lua for script programming, but they have never tried it.
As a serious vim + python enthusiast, I am using vim to directly execute python commands (such as the famous pyflakes plug-in) every day. I want to know how vim is implemented, so I tried it myself.
Because there is no linux development environment in the house, Mingw + makefile is developed in windows.
First write a simple py program: helloworld. py

12
def hello():       print"hello,world!"

Then write our c ++ code: py. cpp

1234567891011121314
# Include <Python. h> // all the preceding configurations are made to call this header file and the relevant database int main () {Py_Initialize (); // before using python, call Py_Initialize (); this function initializes PyRun_SimpleString ("import sys"); PyRun_SimpleString ("sys. path. append ('. /') "); PyObject * pModule = NULL; // declare the variable PyObject * pFunc = NULL; // declare the variable pModule = PyImport_ImportModule (" helloworld "); // here is the name of the file to be called pFunc = PyObject_GetAttrString (pModule, "hello"); // here is the name of the function to be called PyEval_CallObject (pFunc, NULL); // call the function Py_Finalize (); // call Py_Finalize, which corresponds to the root Py_Initialize. Return 0 ;}

Then makefile:

12345678910111213141516171819
CXX = g++ TARGET = py C_FLAGS += -g -Wall INC = -IC:/Python27/include/LIB = -LC:/Python27/libs/ -lpython27 all: $(TARGET) py:  py.o$(CXX) -o $@ $^  $(LIB) $(C_FLAGS) .cpp.o:$(CXX) -c -o $*.o $(INC) $(C_FLAGS) $*.cpp clean:-rm -f *.o $(TARGET)

The execution result is as follows:

C:\WINDOWS\system32\cmd.exe /c pyhello,world!

At present, I just studied it briefly. I will try again later.
Code path: http://code.google.com/p/vimercode/source/browse/#svn/trunk/cpppy
There are several more detailed articles on the Internet. If you are interested, you can take a look.
C ++ calls Python (3)
C ++ calls Python (4)
-------------------------------------------------------------------
Makefile in linux is as follows:

12345678910111213141516171819
CXX = g++  TARGET = test_py C_FLAGS += -g -Wall -pthread INC = -I/usr/local/include/python2.7/LIB = -L/usr/local/lib/ -lpython2.7 -ldl -lutil all: $(TARGET) test_py:  test_py.o    $(CXX) -o $@ $^  $(LIB) $(C_FLAGS) .cpp.o:    $(CXX) -c -o $*.o $(INC) $(C_FLAGS) $*.cpp clean:    -rm -f *.o $(TARGET)

This program runs core in linux. You can try to read this article:
Http://blog.csdn.net/marising/archive/2008/09/12/2917892.aspx

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.