Getting ready

We will try a slightly more involved embedding example in a C++ program. The example is again taken from the Python online documentation (https://docs.python.org/3.5/extending/embedding.html#pure-embedding) and shows how to execute functions from a user-defined Python module by calling the compiled C++ executable.

The Python 3 example code (Py3-pure-embedding.cpp) contains the following source code (see https://docs.python.org/2/extending/embedding.html#pure-embedding for the corresponding Python 2 equivalent):

#include <Python.h>int main(int argc, char *argv[]) {  PyObject *pName, *pModule, *pDict, *pFunc;  PyObject *pArgs, *pValue;  int i;  if (argc < 3) { fprintf(stderr, "Usage: pure-embedding pythonfile funcname [args]\n"); ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.