1. c. Call Python
In fact, the interaction between C and python is very simple. The simplest example is as follows:
1 Py_initialize ();2 3Pyrun_simplestring ("STR = \ "Hello \"");4Pyrun_simplestring ("Print Str");5 6Py_finalize ();
Like C's preparations before entering the main functionProgramIt starts with py_initialize (). It is nothing more than applying for some prepared resources. It ends with py_finalize () to release the applied resources, after the main function is completed, the C program will apply for the release of resources, but the C program is handled by the compiler.
The aboveCodeIn fact, it is input in the python interpreter.
STR = "hello"
Print Str
Similar to using idle for Input
When we call a function, we need to know the function name, function parameters, and function return values. This is also the case when C calls a python function.
Python program:
1 DefHello (I ):2PrintI3ReturnI
1 Py_initialize (); 2 3 Pyobject * pymod = pyimport_importmodule ( " Test2 " ); 4 Pyobject * pyfunc = pyobject_getattrstring (pymod, " Hello " ); 5 6 Pyobject * pyparams = pytuple_new ( 1 ); // Apply for tuple to pass in Parameters 7 Pyobject * pyvalue = pystring_fromstring ( " 2 " ); 8 Pytuple_setitem (pyparams, 0 , Pyvalue ); 9 10 Pyvalue = Pyobject_callobject (pyfunc, pyparams ); 11 12 If (Pyvalue) 13 { 14 Int Pret = Pyfloat_asdouble (pyvalue ); 16 } 17 Py_finalize ();
When calling a python function, we need to know the module where the function is located. This is the role of pyimport_importmodule and introduces the Test2 module (why is it not test directly because Python has test itself, if you introduce test, you will not be able to find the hello function), which is the same as the import in the python program. Then we use pyobject_getattrstring to obtain the function in this module, if we need to input parameters, we can use tuple in Python, which reminds me of Erlang. In eshell, parameters are usually transmitted through the list. Then, we call the function through pyobject_callobject, the Return Value of the function is stored in the return value of pyobject_callobject. We can obtain the return value through pyfloat_asdouble (the return value is 2)
In this way, the interaction between C and Python programs is completed. Of course, I did not check the Errors for convenience.
As for the Python script, I recommend that you do not write the interface too complicated, just like the interface that Erlang recommends to others. Generally, start and end are used in a module, in this way, debugging is convenient even if an error occurs in the future.
In addition, you do not have to provide the py file when providing it to users. You only need to provide the PyC file.
2. Python calls the C program
In fact, you can directly read the collshell blog. Here is a simple example.
Http://coolshell.cn/articles/671.html
However, in Windows, you must note that if your compilation file ends with CPP, you must add extern "C" before the function. If C ++ is familiar, you should know why, after all, C ++ must support function overloading.