Calling python from a c++ program for distribution

前端 未结 6 2248
北海茫月
北海茫月 2020-11-28 02:47

I would like to call python script files from my c++ program.

I am not sure that the people I will distribute to will have python installed.

Basically I\'m

6条回答
  •  青春惊慌失措
    2020-11-28 02:56

    Interestingly, nobody has mentioned pybind11, yet. From their documentation:

    pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code. Its goals and syntax are similar to the excellent Boost.Python library by David Abrahams: to minimize boilerplate code in traditional extension modules by inferring type information using compile-time introspection. [...] Since its creation, this library has grown beyond Boost.Python in many ways, leading to dramatically simpler binding code in many common situations.

    Concretely, calling into a Python function (called embedding) is as simple as this (taken from the documentation):

    #include  // everything needed for embedding
    namespace py = pybind11;
    
    int main() {
        py::scoped_interpreter guard{}; // start the interpreter and keep it alive
        py::print("Hello, World!"); // use the Python API
    }
    

提交回复
热议问题