The Challenge of Cross-Language Interoperability 286
CowboyRobot writes "David Chisnall of the University of Cambridge describes how interfacing between languages is increasingly important. You can no longer expect a nontrivial application to be written in a single language. High-level languages typically call code written in lower-level languages as part of their standard libraries (for example, GUI rendering), but adding calls can be difficult. In particular, interfaces between two languages that are not C are often difficult to construct. Even relatively simple examples, such as bridging between C++ and Java, are not typically handled automatically and require a C interface. The problem of interfacing between languages is going to become increasingly important to compiler writers over the coming years."
Cross language - what .Net gets right (Score:5, Insightful)
Re: (Score:3, Informative)
This was one years before then, it used to be normal to be cross language. Ie, VMS was implemented in a variety of languages, Unix provided a common calling standard between languages, etc.
Re:Cross language - what .Net gets right (Score:4, Informative)
(Hmm... I thought the lion's share (if not all) of VMS was written in BLISS.)
You could definitely call, say, a VAX BASIC routine from a VAX FORTRAN program, VAX FORTRAN subroutines from VAX C programs, etc. And you didn't have to jump through a bunch of hoops to do it, either.
Re:Cross language - what .Net gets right (Score:5, Informative)
An early decision in the design of VAX/VMS was the definition of the "VAX Procedure Calling Standard" that dictated the instructions and mechanisms to be used for calling procedures, passing parameters and returning values, independent of language. All the compilers were expected to use this mechanism so that you could, for example, call a procedure written in VAX COBOL from VAX FORTRAN. This worked to a large extent, but it wasn't explicitly defined (and couldn't really be defined) whether compilers should use call-by-value, call-by-reference or call-by-descriptor for particular data types so additional semantic cruft was required to sort out the deails of parameter-passing. VAX C would sometimes pass a double-word argument in violation of the standard. The standard also had nothing to say about meta issues like run time initialisation, memory and thread usage, etc.
That said, it was a revelation coming from an IBM world in which you'd sometimes have to write Assembler shims to patch up the calling conventions if you needed to get one language talking to another.
Re: (Score:2)
Outside the kernel, many of the VMS commands and utilities were written in a mish mash of languages, as if each programmer just chose what they wanted to use. BLISS and Fortran and Macro-32 all in the same program. There was also a data description language added to the mix to allow sharing data structures across languages.
Re: (Score:3)
many of the VMS commands and utilities were written in a mish mash of languages, as if each programmer just chose what they wanted to use
If they chose languages that were better for the task at hand than others, that sounds like a good approach. VAX cross-language compatibility made it practical.
BLISS and Fortran and Macro-32 all in the same program.
Mixing BLISS and Macro-32 is hardly a kluge, but what utility mixed them with Fortran?
There was also a data description language added to the mix to allow sharing data structures across languages.
Another excellent idea, supporting cross-language operation.
Between VMS and Alpha DEC manage to prove the old adage: if you build a better mousetrap, your company will go down the toilet. More recent companies learned from that, and avoid well thought out designs.
Re:Cross language - what .Net gets right (Score:5, Informative)
VMS managed to get the idea of the platform ABI specifying procedure call conventions right very early on. It had quite an easy job though. C, BASIC and Fortran are all structured programming languages with basically the same set of primitive types. None of them have (or, in the VMS days, had) classes, late binding, or real garbage collection. BASIC is kind-of GC'd, but it doesn't have pointers and so everything passed across the language barrier from BASIC was by value, so the GC didn't have to do anything clever.
It's worth remembering that when VMS was introduced, other platforms were still having problems getting C and Pascal to play nicely together (Pascal pushing arguments onto the stack in the opposite order to C), so that's not to belittle the achievement of VMS, but it's a very different world now that we have Simula and Smalltalk families of object orientation, various branches of functional languages, languages like Go and Erlang with (very different) first-class parallelism, and so on.
Re: (Score:2)
Absolutely, but only as long as you cross languages that all compile to CIL which is a language in itself.
Re:Cross language - what .Net gets right (Score:5, Informative)
Re: (Score:2)
That actually sounds really cool. Must take a closer look at .NET now. :-)
Re:Cross language - what .Net gets right (Score:4, Insightful)
don't - although the COM interop tooling for VB.NET is quite good, its all a massive performance penalty, and isn't quite as nice as its presented. The cross environment marshalling that's required (plus some pinning of data so the garbage collector becomes less efficient) means its there for convenience only. You wouldn't want to use it for heavy applications.
Microsoft wants you to rewrite in .net (well, then they did, now they want you to rewrite it all in WinRT).
Re:Cross language - what .Net gets right (Score:4, Informative)
Coming from the Java world, I was totally blown away by what
Re: (Score:3)
The cross environment marshalling that's required (plus some pinning of data so the garbage collector becomes less efficient) means its there for convenience only. You wouldn't want to use it for heavy applications.
That isn't really true. Most of Microsoft's own "heavy applications" use it, heavily in fact. Both Office and Visual Studio use it a lot, especially the latter.
Yes, it's slower than .NET-to-.NET, much less native-to-native. But any bridge is going to introduce overhead, when either side speaks a different language, utilizes different data structures etc. In that sense, P/Invoke is actually pretty slim, because so long as you use "blittable types", i.e. primitives and structs thereof - basically things that
Re: (Score:2)
If Microsoft "won" that round, why have they almost completely abandoned it in recent years?
If you have tried writing a Metro app with any reasonably proprietary support for media capabilities, it's a convoluted nightmare of "unsupported APIs", media pipelines, etc just to do anything interesting. And that's just Windows 8 on a PC! As much as they pretend Xbox One is "the same" as Windows 8, it's not, and requires a whole new level of bending over backwards to accomplish anything.
Re: (Score:2)
Modern apps (the new name of metro) use WinRT, which is NOT a complete CLI environment. It has a subset of the CLR classes, and then adds some proprietary extensions to it. It's designed to ease the walled garden lock-in, not to improve the programming experience.
If .NET has been neglected somewhat, it's simply because it's not profitable enough to justify any more spending on the platform.
Re: (Score:2)
Yes, that's exactly my point (and I'm going to continue to say "Metro" with impunity - do you work for Microsoft or just a party-liner? ;) - WinRT is not .Net, MS has embraced WinRT (or even a *subset* of WinRT on non-PC platforms), ie. WHAT in effect have they "won" with this platform in the *long run*?
Re: (Score:2)
WinRT, which is NOT a complete CLI environment
That's sort of a logical consequence of the fact that perhaps with the exception of the metadata format, WinRT has nothing to do at all with CLI. Therefore, it's not even a partial CLI environment, much less a complete one!
Re: (Score:3, Interesting)
.NET is already an extremely verbose platform that is many years ahead of its competition. If it seems like they've neglected it a bit that may be because there's currently either no motivation to add new features to it, or there's currently not enough features worth adding to it that would justify an incremental release and all of the accompanying documentation. I'd rather that they take a step back, let it mature a bit, and clean things up a bit if necessary.
Re:Cross language - what .Net gets right (Score:5, Insightful)
.NET is already an extremely verbose platform that is many years ahead of its competition
Wha? Its "competition" for what, exactly? Windows apps? So, wait, you are telling me a Microsoft development platform is ahead of its competition for developing Windows apps? And how is that interesting?
Re: (Score:2)
Re: (Score:2)
If Microsoft "won" that round, why have they almost completely abandoned it in recent years?
The .NET Framework was last updated 47 days ago (as of this post). That's so far from 'abandoned', I wonder if you're just trolling (which you almost certainly are).
Re: (Score:2)
.NET backend + javascript front end, job done. It's mature, feature complete almost to exhaustion, and turns modern-day developers to focus on business requirements and integration.
Re: (Score:2)
It's mature, feature complete almost to exhaustion, and turns modern-day developers to focus on business requirements and integration.
You could say the same thing about Franz Allegro CL, or pretty much any established environment for that matter.
Re: (Score:2)
Re: (Score:2)
Personally I always preferred calling C functions from machine code it's easy just push your parameters or pointers to your parameters onto the stack
...except for the ones you pass in registers.
Re: (Score:2)
Re:Cross language - what .Net gets right (Score:4, Insightful)
Passing in registers is not a standard C parameter passing method
There's no such thing as "a standard C parameter passing method". Passing in registers is a perfectly legitimate C parameter passing method, used on several RISC architectures, such as SPARC [oracle.com], MIPS [sgi.com], 32-bit ARM [arm.com], 64-bit ARM [arm.com], and 64-bit {PowerPC/Power Architecture} [ibm.com] (and probably most other RISC architectures), as well as x86-64 [x86-64.org] and z/Architecture [redhat.com].
If there are more parameters than fit in the registers available for parameter passing, or if the parameters are in the variable-length portion of the argument list, they might be passed on the stack, and if the called function has no prototype in scope, the compiler might be forced to pass everything on the stack, but, in all other cases, if the ABI supports it, parameters can and will be passed on the stack.
Re: (Score:2)
Passing in registers is not a standard C parameter passing method
So, when are you going to finally upgrade to AMD64? ;-)
Re:Cross language - what .Net gets right (Score:5, Insightful)
Re: (Score:2)
.net did not get it right - they basically said "there is 1 language, the CLR and it runs in its own little sandbox. We might add some crappy hooks to let others play with us, but they'll be terribly inefficient and difficult to use".
Its the same with Java - the environment is almost deliberately designed to be "java only".
If .net had built its tooling to run natively instead of in the managed sandbox then I think they could have done better with the interoperability. As it is, they really want you to becom
Re: (Score:2)
I don't even like .Net, but they won this round years ago.
I think you have misspelled "Scheme".
Java, all you need. (Score:5, Funny)
What do you need multiple languages for anyway? Java does everything you could want. Java is a powerful, object-orientated, cross-platform language, with fully developed GUIs, such as Swing.
To demonstrate the superiority of Java, I can point to such leaders in their field as Eclipse, Minecraft, and this awesome applet I saw on someone's homepage once.
Anyone still using ancient and difficult to use languages such as C++ (let alone C!) are obviously crazy, and probably should be committed for their own good before they go on spree of shooting (not just themselves, but) other people in the foot. Java makes it almost impossible to shoot yourself, let alone others, in the foot.
Moreover, because Java is licensed under the GNU GPL, you can leverage the wisdom of crowds, and the powerful "many eyes make bugs shallow" concept to be confident that Java is the best.
And with just-in-time, Java is as fast as any other language, so you don't have to worry about the speed of execution. Instead, you can focus on developer time, and Java's just faster in that regard.
With built-in, from the ground up, support for Unicode, Java is there for the future, and is ready to be used across the multiverse (as soon as those aliens get their scripts into Unicode). Beat that C, with your lack of a string type.
And if you aren't convinced, tell me why do so many top enterprises use this language? You don't see ads from Fortune 500 companies looking for Ruby "developers" do you?
Java, a horrible horrible language. (Score:2)
I'm sitting right now and coding an interface between Java and Erlang using JInterface, and the actual JInterface part is pretty good. It makes Java act as an Erlang node. But Java is just a horrible, horrible language.
Everything on the Java side gets so bloated it is just silly. What would take one line on the Erlang side can easily take 20 lines on the Java side.
I know you are just trolling, but...
Swing is the most horrible toolkit I've ever tried to use. Even monkeys can make a better one.
Eclipse is neve
Re: Java, a horrible horrible language. (Score:2)
At least the language problems can be solved by using another language that compiles to Java byte code. Scala for example, which should be somewhat better in term of bloat size.
Re: (Score:2)
IDE:
Eventually, projects get complex and you do need ide not to tame language but to keep being productive without having to cross check documentation of different modules. But have fun managing to write ~150 entities relating project in plain text.
Verbosity:
1-line-in-20 is usually just lack of knowledge of how to do something right in one language paired abuse of highly specialized construct in another.
I have seen people complain how terrible i.e. (regxex, xml, etc.) is in java, but their problem was solve
Re: (Score:2)
Re: (Score:2)
Eclipse is never needed for a real language, if you need an IDE, there is something wrong with your language.
Oh your one of those people.....
Re: (Score:2)
There's a difference between needing an IDE and simply preferring one. With other languages I like a good IDE, but I've worked on large projects without one, and it's not terribly painful. With a Java project beyond trivial size it feels like you just can't function without an IDE. That's bad language design.
Re:Java, all you need. (Score:5, Insightful)
Clearly there is some sarcasm/irony, as "What do you need multiple languages for anyway?", "and this awesome applet I saw on someone's homepage once" is pretty clear (unless he's a moron).
But honestly the 80% of the rest of his points are actually true about Java - so either he doesn't really understand irony, he doesn't really understand Java, or he is just trying (rather successfully, IMO) to troll slashdot. I'm hoping that last case, in which I salute his efforts :)
Re: (Score:2)
Does posting as an AC make you less of a prejudiced a-hole? No, I think not. Any possible points you may get for pointing out what has already been pointed out was lost when you decided you needed to stereotype 300 million people. But please come back when your balls drop.
Re: (Score:2)
Wait, upon rereading, I don't think that GP is actually being sarcastic. Can someone with a better online sarcasm detector please jump in here?
Either you have a dry sense of humor, or this is Slashdot.
Re: (Score:2)
but in order for java to be cross platform
Java is portable in the same sense that you'd be portable if you traveled with your own hotel.
parent is obviously trolling...
Or parodying.
Re: (Score:3)
Java, C++ (Score:5, Informative)
The fault here lies specifically with Java and C++. Java's JNI is poorly designed and makes no serious attempt to make interoperability easy because it is against Java's philosophy. C++'s runtime is deliberately completely implementation dependent because C++'s designers thought that might let implementors squeeze out a little bit of performance.
Nevertheless, tools like Swig make even Java/C++ interoperation fairly easy, and many other languages try to accommodate C++ well (cf. Boost::Python).
Re:Java, C++ (Score:4, Insightful)
Java's JNI is poorly designed and makes no serious attempt to make interoperability easy because it is against Java's philosophy
I'm not an expert but I've done a bit of native language binding now and again. I didn't find JNI a massive barrel of laughs, but I didn't find it notably worse than other native bindings (e.g. CPython, ...). I was able to make calls in and out and throw java exceptions etc etc easily enough.
Can you be more specific without reference to higher level wrapper tools (e.g. Boost:Python) since those aren't really part of the languages.
C++'s runtime is deliberately completely implementation dependent because C++'s designers thought that might let implementors squeeze out a little bit of performance.
They made the right choice here. C++ compiles have come on an awfully long way since the beginning. One of the biggest parts of the runtime (exceptions) have gone from slow and painful to essentially zero cost if you never use them. That ould never have happened if the rnutime was heavily specified.
Besides, C++ is too portable for uch specification not to be quite harmful.
Re: (Score:2)
It would have been nice however if the C++ committee had specified a standard mangled format for class methods in binaries.
eg:
class foo
{
void myfunc() { }
};
will ALWAYS have the symbol name something like "__cpp_foo_myfunc" in the object file with the first parameter being a pointer to an object - ie "this". Instead of different compilers mangling the name in their own way. That way you could dlopen() a c++ binary with ease and use the methods directly.
Re: (Score:2)
In Python and other HLLs, you can do all the C binding in the HLL itself, allowing you to call most shared libraries directly. In Java, you always have an extra function call overhead and you need to distribute an extra native code library.
Really? For every single feature in the language that's unspecified, leaving it unspecified and up to the impleme
Re: (Score:3)
In Python and other HLLs, you can do all the C binding in the HLL itself,
If by that you mean something like CTypes in Python, the same existst in Java too: JNA.
you need to distribute an extra native code library.
Yes, but that's not always a big problem. I've only ever used JNI to interface to a big blob of C++ code that I wrote in C++ for speed (and because it's a nicer language for this sort of thing than Java). In that case, the binary has to be distributed no matter what the host language.
Really? For ev
Re: (Score:2)
In Python and other HLLs, you can do all the C binding in the HLL itself, allowing you to call most shared libraries directly. In Java, you always have an extra function call overhead and you need to distribute an extra native code library.
On the other hand, general function call overhead is higher in Python than in Java (for various good reasons!) so arguably what's actually happening is that you're just having to introduce the cost in Java that was there all along in a higher-level language.
If you're really worried about the cost of calling, measure it and see whether it really matters.
Re: (Score:2)
One of the biggest parts of the runtime (exceptions) have gone from slow and painful to essentially zero cost if you never use them.
I've heard that before and am seriously interested in 2 questions:
1. What changed to make exceptions efficient? I know it's a a compiler issue, but if anyone is knowledgeable about the details or has a link, I'd be very interested.
2. Any benchmarks, cites, whatever?
#2 is not about skepticism, but being able to shove documentation under someone else's nose. I do a lot of "deeply" embedded stuff (no OS - RTOS or even bare metal, extremely low latency requirements, extremely performance critical in some areas)
Re: (Score:2)
One of the biggest parts of the runtime (exceptions) have gone from slow and painful to essentially zero cost if you never use them.
I've heard that before and am seriously interested in 2 questions:
1. What changed to make exceptions efficient? I know it's a a compiler issue, but if anyone is knowledgeable about the details or has a link, I'd be very interested.
2. Any benchmarks, cites, whatever?
#2 is not about skepticism, but being able to shove documentation under someone else's nose. I do a lot of "deeply" embedded stuff (no OS - RTOS or even bare metal, extremely low latency requirements, extremely performance critical in some areas) and the standard rule for years has been to turn off exceptions. Many compilers for embedded work even do that by default. I'd love to be able to change that, since exceptions make error handling so much easier.
Just thought of something else: even if modern compilers have improved, it's far from a guarantee that ones for embedded use have. At least gcc should though, and that's used for some embedded work.
Your Mileage May Vary. I recommend doing performance tests. It doesn't matter how any other system handles exceptions. Only the ones that you use yourself.
Re: (Score:2)
1. What changed to make exceptions efficient? I know it's a a compiler issue, but if anyone is knowledgeable about the details or has a link, I'd be very interested.
I believe the old way was to add stuff to the stack frame in order to track things. This obviously added overhead. The new way was to build a lookup system that takes the PC and uses that to jump into the unwind code. I think the lookup of the PC is quite slow, but it does not add any cost on the hot path.
In terms of benchmarks, try running the
Re: (Score:2)
I believe the old way was to add stuff to the stack frame in order to track things. This obviously added overhead. The new way was to build a lookup system that takes the PC and uses that to jump into the unwind code. I think the lookup of the PC is quite slow, but it does not add any cost on the hot path.
Nice approach. Who cares how long it takes once you've thrown an exception?
In terms of embedded code... yeah dunno.
Ideally what I'd like is to have the lowest, most speed critical code, not throw exceptions or incur any overhead due to their existence. I know that a function that doesn't throw any exceptions itself still has to have some provision for dealing with exceptions, but suppose you could guarantee that function foo() throws no exceptions and that none of the functions it calls do. Would a nothrow decl for foo(), and no catch statement i
Re: (Score:2)
I would be very careful with claims about poor design of JNI. There are multiple aspects which are non-obvious to person who 'just want to call that single native call without hassle'.
Example - think about implementing GC which is concurrent and copying. You want to perform it while some piece of code is inside JNI call (it might stay there forever, for example waiting for I/O). That bit of JNI was passed references to few of your java objects. Suddenly, a lot of magic around JNI local and global references
Re: (Score:2)
I am careful about such claims, having used native code interfaces since long before JNI was even conceived. Dealing with resource management and relocating garbage collection isn't rocket science, and JNI does it poorly.
Part of the problem is that Java refuses to provide unsafe primitives in the Java language itself, for no good reason. That means that all the heavy lifting needs to be done in C. Most native code calling shouldn't require writing any wrappers; a little preparation in the HLL followed by a
Re: (Score:2)
Java refuses to provide unsafe primitives in the Java language itself
They're trying to save you from yourself, or from the most inept programmer you have. Sorry folks, there is no such thing as an idiot proof language. A simple and effective approach, which other languages use, is to have anything that's unsafe include "unsafe" in their name. Then you can find where it's used with a simple text search (or is that too simple for a modern IDE?).
Re: (Score:3)
Part of the problem is that Java refuses to provide unsafe primitives in the Java language itself, for no good reason.
There's a reason. Java wasn't designed to be unsafe. It exerts a lot of effort to avoid being unsafe, which is why things like buffer overflows are not routine exploits in Java code. It also takes the "write once/run anywhere" concept very, very seriously. Once you reach the JNI level, of course, all bets are off, but again, Java is primarily self-contained.
If these features are an impediment, Java isn't the best platform to be using.
Sort of (Score:3)
I suspect that you would find yourself having to learn Haskell for one emergency and Erlang for the next.
Where a multi language compatible API would be great though is when you start to migrate your system from one language to another. If you could do it piece by piece deploying each small well tested piece before moving on to the next I would suspect that many a disaster would be avoided.
Interfacing 2 languages that are C *is* easy (Score:2)
In particular, interfaces between two languages that are not C are often difficult to construct
(Yes, I know what they mean. I think they meant to write "In particular, interfaces between two languages (when neither is C) are often difficult to construct". :)
Still, it amuses me to think about making an interface from C... to C
Re: (Score:2)
Interfacing 2 languages that are C *is* easy
You have a different definition of easy than I do.
Interfacing from X to C is usually *possible*.
Re: (Score:2)
Re: (Score:2)
Re: (Score:3)
I just tried working with a codebase that had the same arrays storing both ints and pointers, and doing index arithmetic all over the place. The really horrifying thing was that it actually compiled 64bit, and just behaved differently.
Well, as Bill Clinton so eloquently said:
"It depends on what the meaning of the word ints ints"
Sockets (Score:5, Insightful)
Use sockets. In majority of cases the performance is more than good enough, especially if designed properly, and you get network transparency "for free".
Sure there are cases where sockets are not appropriate, but IMHO they are far too seldom used.
Re: (Score:2)
Use sockets. In majority of cases the performance is more than good enough, especially if designed properly, and you get network transparency "for free".
Sure there are cases where sockets are not appropriate, but IMHO they are far too seldom used.
At that point one might as well go with RPC.
Re:Sockets (Score:4, Informative)
We're talking about loading libraries , not inter process communication.
But sure, instead of loading libs written in one language into another language you could just have N processes all written in different languages and communicate that way, but it would be at least an order of magnitude (probably 2) slower than directly linking in a library to your running process.
Scarcity of single host applications (Score:2)
One facet of this is that increasingly, any application of any great interest runs over the network, often using HTTP, so its UI is coded in JavaScript or somesuch and the back end can be any motley arrangement you like that happens to work.
Even traditionally single-host applications like word processors nowadays involve mechanisms for installing software updates and online dictionaries or cloud-based functionality. As for games, you can't even launch most single-player games that should have no need of a n
Python fairs pretty well (Score:3)
On this aspect, Python does handle interoperability pretty well (at least with C and C++). It might just have a little bit too many options: .h-like file
* ctypes: connect to any C library directly (you just have to not do any mistake in parameters, as there is not check)
* Python C extention: write a wrapper in C.
* SWIG: "automatically" generates the wrapper, based on some
* cython: write C code using python syntax
Personally, I just use ctypes or cython, and it's quite easy to interpolate with any software library I need.
Leave windows behind please. (Score:2)
Seriously,
This is the plight of windows ecosystems. Linux/Unix has had abilities in this regard for many many years. It's stable it's rock solid and it performs.
Java is a bit of a nightmare for performance. But a ton of Enterprise is written in Java. Depending on your role you would argue the same thing for python, perl ruby etc. The later languages tend to perform better when calling native libs and vice versa. .NET is an abomination of performance an security disasters. These issues are backed into
Re: (Score:3)
Java is a bit of a nightmare for performance.
Citation needed.
Modern JVMs often out-perform even assembly language due to their ability to analyse and tune for the actual operating environment. Java routinely out-performs C - which like assembly produces static code that cannot automatically tune itself at run time. We're no longer doing interpreters with the old 10-to-1 performance differential that such environments were known for.
True, a small, trivial Java program will perform horribly relative to an equivalent non-VM language. The VM takes a lot
Re: (Score:2)
Of course a JVM out performs assembly. Think about how the assembly was written. True assembly is written by hand. And it's painfully difficult to write.
Java doesn't outperform optimized C or C++ in reality. Java can't even touch languages like perl for manipulating textual data. But you can certainly produce functional code faster in Java than most languages. It really is a great language for getting stuff to market quickly.
My job is performance engineering. This is what I live and breath. Java is a
Re: (Score:3)
When all's said and done, when you put power tools in the hands of the incompetent, you amplify their incompetence.
Most of the examples you listed weren't faults of Java, they're faults of the architects and people like that commit similar atrocities whether in Java, C, or VB. IBM mainframes were performing address-space hopscotch on CICS before Java was even born.
As for WebSphere, IBM isn't as ubiquitous as they used to be. Every few years I have to work with it. The rest of the time I have better tools at
Been there, doing that (Score:3)
Shlaer-Mellor / Executable UML have been offering this type of language independence for over 20 years. The method works from business to embedded software. All that's required is model compiler support for the target language, which can be bought off the shelf or made in-house. Currently model compilers exist for C, C++, Java, Ada, System C, and I'm sure there's more that I haven't encountered.
Re: (Score:2)
I don't even know why anyone would even bother with c++. If it's a good fit, just use c.
Torvalds is right about the language; even though he's right for the wrong reasons.
Re:this kind of comment system is dead (Score:5, Informative)
Because C++ lets people automate resource management and error handling, things that in C are manual and error prone. C++ also provides standards for abstraction, encapsulation, and substitutability, something that in C is not standardized and handled differently by every library. Despite C++'s many flaws and unnecessary complexity, it still is a better language than C for many projects.
Re: (Score:2)
Better as long as you don't want to cross it with a different language I suppose.
Re: (Score:2)
Better as long as you don't want to cross it with a different language I suppose
Er, no.
C++ has C as almost a complete subset. Interfacing other languages is usually done in mostly the C subset. Except you get to wrap other-language structs in an RAII object so you never screw up resource management.
Re: (Score:2)
So what you basically do is interfacing between C++ and C, and then between C and some other language.
Re:this kind of comment system is dead (Score:4, Interesting)
As someone who's done a fair bit of the reverse, and has recently needed to write C bindings for a few other languages (go, ruby), I thought this shouldn't be nearly so hard as you describe. Here's what I've come up with:
Example.cpp: Implements a class and C bindings for that class (The bindings could obviously be in a different file)
#include "ExampleCBindings.h"
#include
class Example {
public:
Example(int v) { value = v; std::cout << "New Example(" << v << ")\n"; }
~Example() { std::cout << "Deleting Example(" << value << ")\n"; }
int getValue() { return value; }
private:
int value;
};
extern "C" {
Example_class* newExample(int value) { return (Example_class*) new Example(value); }
void deleteExample(Example_class* example) { delete (Example*) example; }
int ExampleGetValue(Example_class* example) { return ((Example*) example).getValue(); };
}
ExampleCBindings.h: Public C bindings // what I love most is that Example_t never exists ;)
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Example_t Example_class;
Example_class* newExample(int value);
void deleteExample(Example_class* example);
int ExampleGetValue(Example_class* example);
#ifdef __cplusplus
}
#endif
main.c: Simple C driver program that uses the api
#include "ExampleCBindings.h"
int main() {
Example_class * ex1 = newExample(5);
Example_class * ex2 = newExample(ExampleGetValue(ex1) + 2);
deleteExample(ex1);
deleteExample(ex2);
return 0;
}
Limitations include:
* Does not support objects existing on the stack. You're in C, this is the norm for opaque data structures.
* Need to individually wrap every function. Would need to create separate wrappers for overloaded functions, or write a variadic function that doesn't enforce types (Sigh, neither language supports reflection)
However, if you only wish to implement public functions, writing a script that autogenerates a wrapper like this would be fairly easy (I've done similar for a mocking framework for C code - now that's actually painful (not the script, inserting stubbed functions into a binary)). A little googling came up with a more formal attempt here [dabeaz.com].
Re: (Score:2)
C++ is a superset of C, and none of C++'s runtime features interfere with cross-language interoperability unless you actually use them and expose them. So, you're never worse off using C++, since you can always write your code the same way you would have done so in C and use C++ features only in a way that don't alter interfaces.
The only real disadvantage of C++ over C is the enormous complexity of C++. Functionally, it is superior in pretty much every way.
Re: (Score:2)
That is true, but C++ does it in a way that makes programming dangerous.
That's why the D language was developed [dlang.org]. It's something like C++, but done correctly with a clean sheet of paper. You can even understand the syntax! C++ programmers can easily learn it.
The question is whether D will ever catch on. It's not, or at least doesn't have the image, of a cool new language. The only chances I see for its success are 1) it'll be used on a lot of FOSS projects, so eventually enough people will want to darg it into their day jobs, and/or 2) some well known company starts using it int
Re: (Score:2)
I am not so sure about that. At least, it still is a quite viable platform. You can learn the language and do useful stuff with it, including GUI apps, and you can distribute them without problems. Many of the libs are quite up-to-date.
But who uses it for anything other than legacy code? Even the DoD dropped their mandate 16 years ago. F-22 software is written in Ada, but F-35 software in C++ (remind me to find deep shelter if I'm ever near an F-35 base).
Old languages (even if they've been updated like Ada) that never really caught by a certain point almost always die, even if very slowly. A newer language has a better chance. It's more of a sociological issue than a technical one, but that doesn't alter the reality of it.
Anyway, why did you have to be careful admitting you liked Ada?
Because sometime
Re: (Score:2)
I use VHDL (Ada's little brother). It is a better language, if you like extreme-typing.
The typing stuff is a bit overdone, but I still much prefer it to Verilog. I'd rather spend time fighting the compiler than tracing down bugs in simulation that could have been caught earlier.
But your VHDL program is proven (and then aggressively optimised) by the synthesizer (a compiler that produces a netlist to create electronics/FPGA to run your program), it does take a while for the synthesizer to be done.
Synthesis is a step that simply doesn't exist in software. How does the synthesizer "prove" anything though, other than that you have a synthesizeable design?
Re: (Score:2)
Verilog won the popularity contest
Does that make it a better language?
Also VHDL is still quite popular some places. I've worked for several companies in the last 10-15 years that use it. I suspect VHDL and Verilog will continue to co-exist. As far as tool chains are concerned, they're both essentially front ends. It's not hard to support both, and most do.
Re: (Score:2)
Torvalds is right about the language; even though he's right for the wrong reasons.
Bsically no.
He's a very good kernel engineer and wrote a very good DVCS. But he's full of shit when it comes to C++. Not a single point of his stands up to any sort of logical scrutiny.
I'd be quite surprised if anyone here will make such points either.
Re: (Score:2)
Not a single point of his stands up to any sort of logical scrutiny.
I guess you missed this part: "he's right for the wrong reasons."
I don't know why you're so defensive of the language. C++ hasn't exactly improved over the years.
Re: (Score:3)
I guess you missed this part: "he's right for the wrong reasons."
No, I saw it. He's wrong AND his reasons are crap.
I don't know why you're so defensive of the language.
I'm not. I use C++ a lot and I quite like it. If you have an open mind, I'll happily bore you to death with all the things I hate about C++ and why I wish they were better.
It is however impossible to have a sensible discussion about C++ with people who know almost nothing about it yet strongly hold opinions about it out of some misguided sens
Re: (Score:2)
Not to mention C++14.
Re: (Score:2)
What about C++11?
That's my point entirely. :)
Re: (Score:3)
I didn't sleep well. I'm tired and pissed off. Therefore a great distraction is to start a flame war!
He's a very good kernel engineer
Agreed.
and wrote a very good DVCS
Yes, bad user interfaces and mish-moshes of barely portable shell and C are the hallmarks of a good design. Mercurial is better, and has been chosen by most people/companies that have sat down and tried to make an objective comparison (e.g. Google, Fog Creek). Git is winning out mostly because it's what Linus, the ultimate cool kid, uses, and a lot of geeks still dream of sitting at the cool kids tabl
Re: (Score:2)
What's with classes and objects? You can use that with C, it's just not built into the language.
Re: (Score:2)
And the English language suffices for bad puns (though I understand Mandarin is even better).
Re: (Score:2)
What's with classes and objects? You can use that with C, it's just not built into the language.
Same reason lisp ultimately never catches on. If you work with other programmers then you have to communicate with them.
You can do almost everything in C that you can do in C++, but you have to do it by hand. But in C you have to do everything by hand and everyone does everything slightly differently.
It's useful to have the class notion baked into the language for the same reason it's useful to have the for loop
Re: (Score:2)
Yes, bad user interfaces and mish-moshes of barely portable shell and C are the hallmarks of a good design.
Fair enough. I was not an early adopter. I gather it was really not very good in the early days until someone wrote some moderately sane UI code to go around it. The main reason I use it instead of mercurial is momentum: git is popular with OSS so I had to learn enough to get by. Then it because a question of learning another system (Hg) or sticking with something moderately familiar.
Git is very good
Re: (Score:2)
GTK+ is written in C but it offers bindings for many other languages including C++. This is very easy since C has good interoperability.
Re: (Score:2)
Re: (Score:2)
The joy of a least common denominator.
Re: (Score:2)
I don't like mixed language programming myself, but you may have limited control over. it. Even if you write everything in language X, important off-the-shelf libraries and whatnot may be implemented in language Y.
Re: (Score:2)
I really don't see the need to use any language other than C. It is simple, elegant, straightforward, portable and versatile, it allows you to do everything you need without too much cargo cult and there is an enormous number of tools available.
Similarly I think that these newfangled "word processors" or even text editors are ridiculous. What can't you do with pencil and paper?
I use C daily for deeply embedded stuff (no OS, ultra-low latency, etc.) and it's still the best choice for that and similar work (e.g. kernels). Sometimes I even use assembly language. But for 20 years now C has been an inappropriate language for "general purpose" applications (i.e. don't talk directly to hardware, etc.). It's too primitive and needlessly dangerous. C was o
Re: (Score:3)