Live data from Hacker News

Swig – Connect C/C++ programs with high-level programming languages

swig.org

61–70 of 90 posts

Re: Swig – Connect C/C++ programs with high-level programming languages

#61
post #52

Yeah, I'm never doing that again. I created a binding for the Irrlicht rendering engine with Lua using swig. There were always parsing errors with the header files so I had to create definition files for almost everything. It was a lot of work, but I did finally get it to function. And then a new version of Irrlicht came out, and I basically had to start from scratch. All the definition files no longer matched the he…

Agreed. I had working Swig bindings for Lua to a C data engine. While it worked, it was opaque, required a lot of diving into the Swig code generators, added its own special footguns, and felt like the most dangerous edge in the project.

The next time I wanted to bind Lua to a C++ application, I just hand wrote the bindings. Boring, repetitive, shovel code. Once I had a template, it would take me just a few minutes to hook up new interfaces. If there was ever a problem with one, it was usually a typo and took 30 seconds to find and fix.

Re: Swig – Connect C/C++ programs with high-level programming languages

#63
post #12

I don't think this is the right way: interop with the C ABI, and for the hercules who want to do that with c++, should be native to such programming languages, not from a project on the side.

It is. Python, Java, etc have built-in ways to interface with native code. SWIG is a framework which sits on top of those, and aims to make it easy to do that interfacing across multiple languages.

Re: Swig – Connect C/C++ programs with high-level programming languages

#64
post #52

Yeah, I'm never doing that again. I created a binding for the Irrlicht rendering engine with Lua using swig. There were always parsing errors with the header files so I had to create definition files for almost everything. It was a lot of work, but I did finally get it to function. And then a new version of Irrlicht came out, and I basically had to start from scratch. All the definition files no longer matched the he…

> create a proper C binding to the C++ interface

That's the generally recommended way of exposing your C++ library to any kind of non-C++ code.

I'm not aware of any software which directly helps with that, unfortunately. You either do it manually or write a custom script. Here's a recent example of the latter, from Dear ImGui:

https://github.com/dearimgui/dear_bindings

Re: Swig – Connect C/C++ programs with high-level programming languages

#65
post #64
post #52

Yeah, I'm never doing that again. I created a binding for the Irrlicht rendering engine with Lua using swig. There were always parsing errors with the header files so I had to create definition files for almost everything. It was a lot of work, but I did finally get it to function. And then a new version of Irrlicht came out, and I basically had to start from scratch. All the definition files no longer matched the he…

> create a proper C binding to the C++ interface That's the generally recommended way of exposing your C++ library to any kind of non-C++ code. I'm not aware of any software which directly helps with that, unfortunately. You either do it manually or write a custom script. Here's a recent example of the latter, from Dear ImGui: https://github.com/dearimgui/dear_bindings

Both pybind11 and CLIF help with this and do it very well.

Re: Swig – Connect C/C++ programs with high-level programming languages

#67
post #12

I don't think this is the right way: interop with the C ABI, and for the hercules who want to do that with c++, should be native to such programming languages, not from a project on the side.

Agreed. And I think approaches like python or node, where you need to write some c code against their API, is getting it wrong also. The most smooth interop experience I've ever had is with c#. You just export normal c functions from a dll, and the c# code can call it. All the awkward bits like "how do I marshal a string" happens in the host code, not the native module. The overall experience of that is soooo much be…

Are you sure python needs that? I heard about c-type.

A python native module may be providing python semantics on top of a dynamic library (libressl), but if some python code decide to dynamically load the shared library to make direct C ABI calls, I guess this is built-in, isn't it?

Re: Swig – Connect C/C++ programs with high-level programming languages

#68
post #20

There is also gobject-introspection[0], which is also capable to generate bindings for quite a lot of higher level languages from (gobject based) c-code. I'm always impressed on how simple it is to use an object I defined in c within python. [0]: https://gitlab.gnome.org/GNOME/gobject-introspection

I used and created libraries using g-i (gobject introspect), also bindings for an app to develop plugins in python.

g-i is algo available for JavaScript, in fact gnome-shell it's written in JS

It's extremely easy if you stick to Gobject convention (this is a good thing).

Re: Swig – Connect C/C++ programs with high-level programming languages

#69
Swig is an interesting piece of work. As a project I think it's quite brilliant, I wrote a one-part-of-three-but-never-finished-the-other-two blog post illustrating how nifty I thought it was [1].

In my old age though I've soured on it a little. The original problem I used it for, generating bindings for hundreds of generated pure aggregate data structures (for a serialized data format), is kinda SWIG's best case scenario.

The more complex (or more modern) your code gets, the more involved the types, the more the inherent complexity of the problem will overwhelm even the mighty SWIG and you'll find yourself wishing to use the native C ABI of whatever language you're binding to.

That said, if you're trying to generate bindings for several languages, I think working with SWIG still might be a worthwhile effort.

[1]: https://blog.vito.nyc/posts/swig-part1/

Re: Swig – Connect C/C++ programs with high-level programming languages

#70
post #6

I've used Swig for C++ to Python interop and let me warn you it is a nightmare. The syntax is absurdly terse and convoluted, documentation sucks and good luck trying to search the web for constructs made only of special characters. CLIF is way nicer for C++/Python. If you need other languages then best of luck (Zig, Rust and D are easier than Java and Go at least).

What about PyBind? I've heard great things about the simplicity of using PyBind for this sort of thing.
Post reply on HN