Python script code needs to be mixed with many languages. Among them, C ++ is quite a lot. Let's take a look at how to smoothly use Python script code in C ++. I hope you can find your answers in this article.
To embed Python script code in c ++, except for writing the script engine by yourself, lua and python can be used in c ++. In addition, MonoBind, angelScript library is a number of c ++ script libraries that can be embedded in c ++ for use.
Today, we have attempted to embed Python script code in c ++ (the sample code is in Python-2.5.2 \ Demo \ embed)
- #include <Python.h>
- int main(int argc, char *argv[])
- {
- // Py_NoSiteFlag = 1;
- // Py_SetPythonHome("D:\\usr\\Python"); // PYTHONHOME
- Py_Initialize();
- PyRun_SimpleString("from time import time,ctime\n"
- "print 'Today is',ctime(time())\n");
- Py_Finalize();
- return 0;
- }
During runtime, errors similar to 'import site' failed; use-v for traceback may occur because of the path problem during python import module. there are three ways to solve the problem (previously, setting the environment variable PYTHONPATH seems to be invalid in 2.5 ).
0. uncomment Py_NoSiteFlag = 1;
This is just to cancel the import site. Of course, if you want to import anything in the code, an error will still occur.
A. Set the environment variable PYTHONHOME = D: \ usr \ Python
B. Call the function before calling Py_Initialize.
Py_SetPythonHome ("D :\\ usr \ Python"); // The parameter is the python installation directory.
The above is how to use Python script code in C ++. I hope you will have some gains.