Live data from Hacker News

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

swig.org

11–20 of 90 posts

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

#11

I got a great speedup using C++ with pybind11 for my Python backend, 30x if I remember correctly. It was for an app that detects wether words rhyme. I was about to release it but ditched it as soon as ChatGPT came out (it can find rhymes easily, and sometimes better).

There's nanobind by the same author as pybind11 that's even faster: https://github.com/wjakob/nanobind

Awesome! Didn't know about that. Thanks.

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

#13
post #8
post #3

In my personal experience, to use a very small C library in python in a work project, my life became simpler when I ditched swig and just called the C API from python. Then I had a pure python codebase that I could run on any python version without recompilation, and I no longer had the complication of having to deal with swig's different versions across different distribution releases. For much bigger projects I cou…

How do you call C API from python? Through executable + stdin/stdout?

https://pypi.org/project/cffi/

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

#15
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).

More than a nightmare, just look at their website.

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

#16
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).

The GNU Radio project dumped Swig and replaced it with pybind11 for the same reason. Just a nightmare when things go wrong.

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

#17
I used swig for a long time. It's really handy.

If you're just targetting python, and it's your first venture into building python extensions, it's fantastic. After a while, it's worth trying out building an extension without the use of swig. Much of the time, it's easier to use than swig is - but you do have to implement all the bells and whistles yourself. If you're writing in C or C++, chances are you like that kind of thing anyway.

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

#18
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 better. It also has the added benefit that if you just want to load a third party dynamic lib and call a few functions, you don't necessarily need to write any native code at all. You can just... do it. Then you don't need to set up a native build system, etc. It's really, honestly superior.

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

#19
Curious of the different reasons reasons people are creating Python bindings. Was it because the project started in Python, then when performance became an issue the slow parts were rewritten in C++. Or it started in C++ and Python bindings were added later.

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

#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

Post reply on HN