Run Python Code in C++

Goals

We explore the method to embed python code into C++ . This is useful when we want to integrate some python code snippets into some C++ based systems. ## compile python Here, we compile python into dynamic link libraries. 1. Download python from python.org

Here, we have almost gotten all of our necessary tools ready. However, some 3rd party library is useful when we are developing some programs such as numpy. Numpy is a powerful tool for algebra calculation in python. In order to add numpy support. We should firstly install pip and then use pip to install the numpy library.

Pass Parameters to Python Function

Passing parameters to python is a little bit difficult since we should make the parameters in C++ environment. Fortunately, Python’s C-API provide several useful apis to construct nearly all types of python data with C++.

Conclusion

In this post we illustrate variety of ways to invoke python code in C++ environment. Besides, the method to compile a useable python library is also presented. ## Reference [1] https://docs.python.org/2/c-api/index.html