Live data from Hacker News

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

swig.org

21–30 of 90 posts

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

#21
I used this years ago and the comments reminded me why I never used it again. Common theme in the comments it’s a nightmare. I do recall it being ungodly prickly to get it to work and debug.

Now days unless it is required for performance I doing up grpc to handle library calls. I’m that lazy. Lazier than a node developer that imports the world for a hello world health check.

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

#22
post #8

Earlier quoted context omitted.

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

https://docs.python.org/3/library/ctypes.html

Yep this is how I did it.

Using a subprocess and pipes wouldn't work with a .so file. I'd need to write a C wrapper to implement a way to call functions and marshall parameters in binary…

Basically CORBA over a pipe rather than a socket!

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

#23
post #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.

What's wrong with the website? Seems fine at a glance?

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

#24

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.

We did python bindings to be able to call proprietary libs in test environments.

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

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

Cython is pretty good for C interop. Numpy is implemented in it

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

#26
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?

CPython is written in C, and has an extensive API, also in C. A good number of CPython's stdlib is implemented in C. It's fairly easily to write an extension lib in C or C++ or Rust, even, and have python dynamically load the library. Shared libs have to be linked differently than they normally are as ld.so needs to be able to locate and load any dependencies. Libs are typically referred to as 'dlo's instead of 'so's to make the distinction.

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

#27
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 syntax is terse if you need to do arbitrary stuff with an arbitrary interface. But if you start off keeping SWIG in mind from the beginning and avoid funky features at API level you might be fine by just "importing" your header files in the SWIG-definition file.

See https://projects.om-office.de/frans/swig_demo/-/tree/master/... (disclaimer: very old and special exception handling)

IMHO this is a good practice anyway, because this way you make sure, your API looks the same in every binding.

And this way you get a binding for Python, Java, JavaScript, C#, Lua, and much more

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

#28
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

GObject is pretty cool, allow great interoperability. Used extensively in gstreamer

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

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

For Java (JNI), using SWIG was a very pleasant experience for me. It allowed me to abstract away shared logic to C++ that would otherwise have to be implemented in both Java and Objective-C.

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

#30
I have once made something remotely similar, to interop between C++ and C#: https://github.com/Const-me/ComLightInterop

I took different approach. Because I only needed to support these two languages, there’s no separate interface definition language, and no code generator for interfaces. Instead, users are expected to write both language projections manually.

Then there’s a runtime code generator on the .NET side of the interop which builds runtime callable proxy types for interfaces implemented in C++, also virtual tables for C# objects consumed by C++.

Post reply on HN