用Go + SWIG替换C++

huangapple go评论108阅读模式
英文:

replace c++ with go + swig

问题

我最近在https://softwareengineering.stackexchange.com/questions/129076/go-instead-of-c-c-with-cgo上提出了这个问题,并得到了一些非常有趣的回答。然而,在我的问题中有一个错误:我假设cgo也可以用来访问c++代码,但事实并非如此。相反,你需要使用SWIG。

Go的常见问题解答中说:“cgo程序提供了一种‘外部函数接口’的机制,允许从Go代码中安全地调用C库。SWIG扩展了这种能力,使其适用于C++库。”

我的问题:
使用SWIG + Go可以访问高级的c++框架,比如QT,并且能够提高生产效率吗?我想使用Go作为“脚本语言”来利用c++库。

你有使用go和swig的经验吗?有什么需要注意的问题吗?

**更新/回答:**我也在IRC上问了这个问题,我认为问题已经解决了:

SWIG是一种从其他语言接口到c++代码的相对清晰的方式。不幸的是,将c++的类型匹配到像go这样的语言中可能非常复杂,在大多数情况下,你必须自己指定映射关系。这意味着SWIG是一种很好的方式,可以利用现有的代码库来重用已经编写的算法。然而,将像Qt这样的库映射到go将会花费很长时间。当然,这是可能的,但你不想这样做。

那些来这里寻找使用go进行GUI编程的人可能想尝试go-gtk或者go版本的wxWidgets。

英文:

I recently asked this question https://softwareengineering.stackexchange.com/questions/129076/go-instead-of-c-c-with-cgo and got some very interesting input. However there's a mistake in my question: I assumed cgo could also be used to access c++ code but that's not possible. Instead you need to use SWIG.

The go faq says "The cgo program provides the mechanism for a “foreign function interface” to allow safe calling of C libraries from Go code. SWIG extends this capability to C++ libraries. "

my question:
Is it possible to access high-level c++ frameworks such as QT with SWIG + Go and get productive? I'd like to use Go as a "scripting language" to utilize c++ libraries.

Have you any experience with go and swig? Are there pitfalls I have to be aware of?

Update/Answer: I've asked this over IRC too and I think the question is solved:

SWIG is a rather clean way of interfacing c++ code from other languages. Sadly matching the types of c++ to something like go can be very complex and in most cases you have to specify the mapping yourself. That means that SWIG is a good way to leverage an existing codebase to reuse already written algorithms. However mapping a library like Qt to go will take you ages. Mind it's surely possible but you don't want to do it.

Those of you that came here for gui programming with go might want try go-gtk or the go version of wxWidgets.

答案1

得分: 5

可以吗?是的。

能在相对短的时间内完成吗?不行。

如果你回头看看其他将大型框架尝试抽象化的项目,你会发现大多数都是“不完整的”。你可能可以开始做一些初步的封装工作,但通常即使是解决简单情况的工作也需要时间,尤其是当有大量底层代码需要封装时,即使使用自动化工具(虽然有帮助,但永远不是完整的解决方案)。然后...你会遇到那些需要花费很长时间(至少是非常非常长时间)的讨厌的剩余10%。再考虑一下,首先它是一个不断变化的目标。例如,Qt即将发布下一个重大重写版本。

通常来说,最安全的做法是坚持使用框架设计的编程语言。虽然许多项目本身都有语言扩展。例如,对于Qt,你应该看看QML,它提供了(除了其他功能之外)一个将Qt与JavaScript绑定在一起的方式。有点类似。但它可能满足你的“脚本化”需求。

英文:

Is it possible? Yes.

Can it be done in a reasonably short period of time? No.

If you go back and look at other projects that have taken large frameworks and tried to put an abstraction layer on it, you'll find most are "incomplete". You can probably make a fairly good start and get some initial wrappers in place, but generally even the work to get the simple cases solved takes time when there is a lot of underlying code to wrap, even with automated tools (which help, but are never a complete solution). And then... you get to the nasty remaining 10% that will take you forever (ok, a really really long time at least). And then think about how it's a changing target in the first place. Qt, for example, is about to release the next major rewrite.

Generally, it's safest to stick to the framework language that the framework was designed for. Though many have language extensions within the project itself. For example, for Qt you should check out QML, which provides (among many other things) a javascript binding to Qt. Sort of. But it might meet your "scripting" requirement.

答案2

得分: 2

关于这个问题的相关更新:现在可以使用cgo与C++进行交互,这个功能已经在Go 1.2中合并。然而,它只支持类似C的函数调用,不支持类、方法和C++的其他功能(希望以后能支持)。

英文:

A relevant update on this issue: it is now possible to interact with C++ using cgo with this CL, which is merged for Go 1.2. It is limited, however, to C-like functions calls, and classes, methods and C++ goodies are not supported (yet, I hope).

huangapple
  • 本文由 发表于 2012年1月9日 23:12:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/8791954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定