Live data from Hacker News

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

swig.org

81–90 of 90 posts

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

#81

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

SWIG directors are awesome. It's so powerful having some functions implemented in C++ and some in Java in the same class, and able to call each other willy-nilly.

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

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

> instead of it being a binary choice between high-level and low-level languages, there is a middle ground.

There is: mid-level languages. I was taught (and still think of) C as a mid-level language; things like assembly are low-level languages; C++, Python, Java, etc., are all high-level languages.

But I'm a graybeard, and these are the meanings that I've learned. It's possible that the definitions have shifted and nobody told me.

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

#83
post #50
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.

If it requires explicitly allocating memory, then it is low level to most contemporary programmers.

That doesn't seem like a useful dividing line, if only because it consigns a lot of different languages, with a very wide array of different levels of abstraction, as being "low level". That makes the term "low level" very nearly worthless.

I thought the "level" of a programming language was referring to the level of abstraction it presents rather than how memory allocation is performed. Low-level would be very close to machine language. C and C++ have very different levels of abstraction, for instance, and being able to express that seems useful.

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

#84
post #83
post #50

Earlier quoted context omitted.

If it requires explicitly allocating memory, then it is low level to most contemporary programmers.

That doesn't seem like a useful dividing line, if only because it consigns a lot of different languages, with a very wide array of different levels of abstraction, as being "low level". That makes the term "low level" very nearly worthless. I thought the "level" of a programming language was referring to the level of abstraction it presents rather than how memory allocation is performed. Low-level would be very close…

A language and its libraries will inevitably allow a whole range of abstractions. Also, just about every piece of software has layers above and below. Hence my subjective qualification: "to most contemporary programmers" - to a bunch of which everything below the Javascript engine is low level !

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

#85
post #84
post #83

Earlier quoted context omitted.

That doesn't seem like a useful dividing line, if only because it consigns a lot of different languages, with a very wide array of different levels of abstraction, as being "low level". That makes the term "low level" very nearly worthless. I thought the "level" of a programming language was referring to the level of abstraction it presents rather than how memory allocation is performed. Low-level would be very close…

A language and its libraries will inevitably allow a whole range of abstractions. Also, just about every piece of software has layers above and below. Hence my subjective qualification: "to most contemporary programmers" - to a bunch of which everything below the Javascript engine is low level !

> A language and its libraries will inevitably allow a whole range of abstractions.

True, but that's a different sort of "abstraction" than what I'm talking about. I'm talking about how abstracted the processor itself is. Libraries and the like don't really enter into it -- this is about the language proper.

Machine language has no abstraction whatsoever. What you write is literally the code that the CPU executes.

Assembly language is one higher level of abstraction. There's still mostly a 1 to 1 correspondence to machine language, but some of that gets hidden for human convenience. You're now writing with symbols instead of numeric op codes, and you have concepts like macros, which have no machine language equivalent.

C is a higher level than that. Every C statement can easily be expressed directly in assembly, but more common operations (loops, subroutines, etc) have a shorthand that makes them easier to write. There is no longer a 1 to 1 correspondence with assembly or machine language, but it's not terribly far from assembly. Still, it's abstracted enough that the language is no longer tied to a specific processor.

C++ is yet another level up the ladder. In a sense, C++ is to C what assembly is to machine language.

And so forth.

I suppose that a plausible (but highly imperfect) rule of thumb for how high up the ladder of abstraction a language sits is how many machine language instructions are required to implement a given language construct. The more required, the higher the level of abstraction. High level languages also have a lot more (indeed, mostly consist of) constructs that simply don't exist at the machine language level.

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

#86

I used Swig to connect our C++ API to Ruby back in... '02 (wow, so 21 years ago, time flies!). It worked great. We could try things out in IRB (the Ruby REPL) and it allowed us to quickly write unit tests in Ruby (this was well before things like gtest). There are probably better/easier ways to do that now, but back then Swig was the only game in town and for us it worked out pretty well.

Reminded me of the time when I had to write a piece performance-sensitive code in C++ but I decided that unit tests are not performance sensitive so I wrote them in Go. Naturally I used SWIG. It worked quite well since the C++ code exposed a very simple interface.

I thought I could leave it that way until one day a manager came and questioned why we had terrible unit test coverage. It turned out we had separate tools for measuring code coverage in C++ and Go and it was impossible to make them work together.

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

#87
post #74
post #48

Earlier quoted context omitted.

I'd recommend pybind11 for c/c++ python bridge. Though you have to compile your wheel for every minor python version separately.

cibuildwheel ( https://cibuildwheel.readthedocs.io/en/stable/ ) makes this fairly straightforward.

pffffffffffffff yea right. maybe if you're building some tiny project one like 1 platform. anything serious and you're immediately diving through github issues for solutions to problems and poring over the source of both cibuildwheel and pip.

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

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

That was exactly my experience writing custom GNU Radio blocks for my master's thesis: swig mangling my C++ variables. I have nothing positive to say about it.

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

#89
post #16

Earlier quoted context omitted.

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.

[dead]

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

#90
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 deeply appreciate comments like this. It's frustrating when a tool sounds like the perfect tool for a job, you sink a bunch of time into it, only to discover this kind of scenario. Sometimes there are early indications, but often not. Thanks for sharing your lived experience.
Post reply on HN