Live data from Hacker News

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

swig.org

31–40 of 90 posts

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

#31

I've used Swig to inherit from base classes in Java to implementations in C++. Swig generated the JNI and C++ bindings. Fun times.

Yep! SWIG with Python may be a poor use-case, but it worked really well for me with Java/JNI. This was 10 years ago though!

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

#32

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.

Often also "started in python", then "some 3rd party library needed to add a feature is only available as C/C++".

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

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

SWIG was created 27 years ago. I've used it when it was the only game in town and I'm grateful for it. Now there are many other options, at least for Python. Use the one that suits you best.

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

#34

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.

So far, I have done this mostly to integrate with existing C++ libraries that aren't available in python. For eg. to be able to use some Qt library...

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

#35
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…

[flagged]

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

#36

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.

i have several compiler projects that are 99% C++ (fully functioning APIs etc) but also have python bindings. why? python bindings are by far the best APIs to expose to whatever C/C++ code you have that you want "casual" devs to use. who are casual devs? either people that write code non-professionally or professional devs that pick up your project outside of their day-to-day. there is nothing that beats the ease of use and (today) familiarity that people have with python.

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

#37
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…

It’s highly context dependent. I’ve used stuff like Swig before now to create code that can be shared between multiple different environments. In that scenario the more host side code you’re writing the less effective your solution.

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

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

I've been extremely impressed by pybind11 for c++/python interop.

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

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

You have several options for calling C functions directly from Python without having to start a separate process and communicate with it. For simple jobs when you just want to call a function or two, ctypes from the standard library is a great choice. You load a dynamic library, define the argument and return types of the function you want to call, and call it. It can even handle callbacks from C code into Python.

When you have a lot of C functions to call, having to duplicate all the declarations in Python code gets old. That's where the third-party cffi module comes in. It can parse straightforward C headers and use the appropriate types automatically. However, for complex headers (lots of macros, for example), it has to run a C compiler, which requires the machine which will run the script to have a functioning C toolchain. When using cffi, it can also be pretty easy to crash the interpreter with a use-after-free bug.

If you're already invested in the Cython ecosystem, you can use that for calling into C too. However, getting into Cython just to call a few C functions would be cracking a nut with a sledgehammer.

Finally, the nuclear option is to write a C extension module. Extension modules receive unconverted Python objects directly and can deeply integrate with the CPython interpreter. However, they also need to be compiled for a specific version of CPython, and won't necessarily work in alternative Python implementations. If you can get the job done with ctypes or cffi, you should avoid extension modules.

Post reply on HN