Live data from Hacker News

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

swig.org

51–60 of 90 posts

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

#51
post #46

Who classifies C++ as a low-level programming language? Even C is only being called low-level relatively recently, and it's not universally accepted as such.

You could argue IS NOT (high-level) does not necessarily imply IS (low-level), in which case no-one classified C++ as low-level.

I guess for the large part the inter-op here is from (mostly) interpreted languages, which are certainly higher level than C++.

Maybe instead of it being a binary choice between high-level and low-level languages, there is a middle ground. Fairly easy to argue that as the case.

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

#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 headers so they had to be recreated.

And then a new version of Lua came out and broke modules. The binding was written as a module so that had to be redone too.

I got way more familiar with the internals of swig than I ever wanted to. Each language it supports has it's own little parser/runtime integration, so I had to learn a lot about how swig and lua interact.

If I were to do it again I'd create a proper C binding to the C++ interface, and then bind that to whatever language I wanted. They almost all support C. C++ is a whole other beast.

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

#54
I much prefer to design a C interface by hand around c++ before ever considering exposing to a high level language. Usually it ends up being cleaner to implement the higher language wrapper in that language around the C interface and use idioms that are native to the high level language.

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

#56

Having more than one programming language in a single project is already a bad design choice.

I couldn't disagree more. It's a bad design choice if there isn't a reason to do it, in which case it can be a perfectly valid tradeoff

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

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

I’ve only ever used pybind11 for C++ -> Python interface and must say, it seems pretty good.

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

#59
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.

Right, I'm sure the project achieved great things and I didn't mean to belittle that.

But today I would not recommend anyone start using it if they have a choice, and it seemed prudent to post that warning on a submission of Swig's homepage without context.

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

#60
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 an…

I don't fully agree. Returning unique_ptr, optional or some error sumtype (absl::Status) is perfectly fine non-funky API, but it will require special Swig handling to rewrap.
Post reply on HN