2

There are several packages out there that help in automating the task of writing bindings between C\C++ and other languages.

In my case, I'd like to bind Python, some options for such packages are: SWIG, Boost.Python and Robin.

It seems that the straight forward process is to use these packages to create C\C++ linkable libraries (with mostly static functions) and have the higher language be extended using them.

However, my situation is that I already have a developed working system in C++ therefore plan to embed Python into it so that future development will be in Python.

It's not clear to me how, and if at all possible, to use these packages in helping to extend embedded Python in such a way that the Python code would be able to interact with the various Singleton instances already running in the system, and instantiate C++ classes and interact with them.

What I'm looking for is an insight regarding the design best fitted for this situation.

4

2 に答える 2

2

Boost.pythonを使用すると、特にスマートポインターを使用する場合に、これらの多くのことをすぐに実行できます。PythonのC++クラスから継承し、それらのインスタンスをC ++コードに戻しても、すべてを機能させることができます。さまざまなことを行う方法に関する私のお気に入りのリソースはこれです(特に「ハウツー」セクションをチェックしてください):http ://wiki.python.org/moin/boost.python/ 。

Boost.pythonは、スマートポインターまたは侵入型ポインターを使用している場合に特に適しています。これらは、PyObject参照カウントに透過的に変換されるためです。また、ファクトリ関数をPythonコンストラクターのように見せることも非常に優れているため、非常にクリーンなPythonAPIが作成されます。

スマートポインタを使用していない場合でも、必要なすべてのことを実行できますが、さまざまな返品ポリシーとライフタイムポリシーをいじる必要があり、頭痛の種になる可能性があります。

于 2011-05-11T15:45:17.703 に答える
1

簡単に言うと、最新の代替pybind11があります。

長いバージョン:Pythonも埋め込む必要がありました。C ++ Pythonインターフェースは小さいので、CApiを使用することにしました。それは悪夢であることが判明しました。クラスを公開すると、複雑な定型コードを大量に作成できます。Boost :: Pythonは、読み取り可能なインターフェース定義を使用することにより、これを大幅に回避します。ただし、boostには高度なドキュメントがなく、PythonAPIと呼ぶ必要のあるものがいくつかあることがわかりました。さらに、彼らのビルドシステムは人々に問題を与えるようです。システムが提供するパッケージを使用しているのでわかりません。最後に、ブーストpython fork pybind11を試しましたが、これは本当に便利で、Python Apiの使用の必要性、ラムダを使用する機能、簡単でわかりやすいドキュメントの欠如、自動例外変換など、ブーストのいくつかの欠点を修正していると言わざるを得ません。 。

于 2017-10-15T13:08:08.183 に答える