简体   繁体   English

为什么要使用SWIG通过C接口包装C ++库?

[英]Why wrap a C++ library via a C interface using SWIG?

SWIG novice exploring the feasibility of wrapping a large C++ library primarily for Python access. SWIG新手探索包装主要用于Python访问的大型C ++库的可行性。 Speaking with the developers working on the C++ one has proposed writing a C interop layer that is then wrapped in SWIG. 与开发C ++的开发人员一起提议编写一个C interop层,然后将其包装在SWIG中。

The two potential options are: 两个可能的选择是:

   Base|  Interop  | Scripting Access

============================================ ============================================

1) C++ |    SWIG   | Supported Languages

2) C++ | C | SWIG  | Supported Languages.

Does #2 add some functionality or stability that I am missing? #2会增加一些我缺少的功能或稳定性吗? It looks like a layer of added complexity. 它看起来像是一层增加的复杂性。 Can anyone suggest why a C layer might be the better interface for wrapping in SWIG? 任何人都可以建议为什么C层可能是更好的SWIG包装界面? (In general terms as you haven't seen the library, etc.) (一般来说,你没有看过图书馆等)

SWIG is very powerful and will eliminate a lot of the laborious boilerplate code that you would have to write otherwise. SWIG功能非常强大,它将消除许多您必须编写的费力的样板代码。 Even with boost::python there is such boilerplate code, although far less than doing everything from scratch. 即使使用boost :: python也有这样的样板代码,尽管远比从头做任何事情要少得多。 The nice thing with SWIG is that it also makes it easy to integrate with additional languages. SWIG的优点在于它还可以轻松地与其他语言集成。 For instance I had a C++ library that I was exposing to C# so I could build the GUI in C#/WPF, and with the same SWIG input files, exposed to Lua so that the GUI could be scripted via the C++ layer. 例如,我有一个C ++库,我正在向C#公开,所以我可以在C#/ WPF中构建GUI,并使用相同的SWIG输入文件,暴露给Lua,以便GUI可以通过C ++层编写脚本。 It was awesome! 太棒了!

About the C layer of your #2 option, I don't see the benefit, except if you want to integrate with a language that doesn't support C++, but I am not aware of such language (SWIG supports about 20 and I only know 4 so maybe one of the others has such restriction). 关于你的#2选项的C层,我没有看到好处,除非你想要与不支持C ++的语言集成,但我不知道这种语言(SWIG支持大约20和我只有知道4所以也许其中一个人有这样的限制)。 SWIG is very capable, and can in fact wrap rather complex interfaces. SWIG非常强大,实际上可以包装相当复杂的接口。 With SWIG you can completely change the API if you wish using the %inline and %extend directives. 使用SWIG,如果您希望使用%inline和%extend指令,则可以完全更改API。 I can't think of a reason that a C layer would be required. 我想不出需要C层的原因。

看看直接使用C ++的Boost Python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM