Import C++ function into Python program

后端 未结 5 861
情书的邮戳
情书的邮戳 2020-12-04 21:59

I\'m experimenting with python functions right now. I\'ve found a way to import python functions into c/c++ code, but not the other way around.

I have a c++ program

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 22:52

    You need to create a python module with that function in it. There are three main ways:

    1. Using Swig - this reads your c code and creates a python module from it.
    2. Hand coded using the python c api.
    3. Using Boost::Python (often the easiest way).

    This pdf covers 1 and 2. This page will tell you how to use Boost::Python.

    You cannot (easily) use a function that is in a c/c++ program - it must be in a static library (which you can also link your c/c++ program against).

    EDIT - Cython Is also worth a mention.

提交回复
热议问题