views:

503

answers:

6

Is it possible to compile C++ program into some intermediate stage (similar to bytecode in java) where the output is platform independent and than later compile/link at runtime to run in native (platform dependent) code? If answer is no, why?

+14  A: 

It is indeed possible, see for example LLVM.

Alex Martelli
Thx for the link. I am accepting you answer.
rjoshi
There's also Adobes C++ compiler which outputs Flash bytecode.
jalf
@jalf - how come I never heard of that till now? Something new to play with... at least it's new to me.
Michael Burr
+5  A: 

This is trivial, and most compilers already do that. gcc compiles to RTL (register transfer language) which is then translated to the target CPU.

Similarly, managed C++ and C++/CLI are compiled to .NET.

Finally you can consider the Church Turing thesis that is a statement of equivalence of programming languages, so C++ can be compiled/translated to your favorite platform independent language (say, Perl, lisp, C--, etc).

plinth
Church-Turing thesis is not applicable here, because people expect far more of any programming language than merely calculating computable functions (except maybe if that language is APL). For instance in J2SE Java I can draw on the screen, but in standard C++ I can't. Once you consider the environment as well as what might be thought of as the "pure" language, the capabilities of those languages are not the same.
Steve Jessop
+2  A: 

Parrot project will have c++ bytecode compilation and execution parrot Visual Studio can compile C++ as bytecode C++ managed

SomeUser
Just remember that Managed C++ is _not_ C++. It's another language, that resembles C++ (like C# or Java do)
Massa
Managed C++ is not C++; it's also obsolete. Managed C++ was replaced by C++/CLI, which is *about* as close to a conforming implementation of C++ as using Microsoft C++ to compile directly to machine code. Admittedly, there are other compilers with better conformance (e.g. Comeau) bit Microsoft's is (now) competitive, and (purely in terms of conformance) there's not a huge difference between compiling to intermediate code and to machine code.
Jerry Coffin
+8  A: 

Of course. Keep in mind that the C++ standard only specifies behavior: What should happen when this program executes. It doesn't specify how it should be implemented.

C++ code can be compiled to an intermediate format and JIT'ed to machine code, or it can be interpreted or anything else you like.

jalf
+3  A: 

C++ source code (with some restrictions) is a platform-independent bytecode.

Why is it not?

Indeed, "bytecode" compilation procedure is then mere copying. The virtual machine that runs the "bytecode" is C++ compiler and a wrapper script. Yeah, it does some stuff that resembles compilation to machine code--but that's an implementation detail.

Here's a Linux implementation of such a "C++ virtual machine":

#/bin/sh

tmp=`mktemp`
g++ $1 -o $tmp && $tmp $2 $3 $4 ...

Does it answer the question? I think, it does. To the extent how specific the question is. Because it clearly explains theoretical possibility of compiling C++ into bytecode. Practical implementations also exist, for example, LLVM.

Pavel Shved
Thanks for explaining in detail.
rjoshi
Smartass answer. True, more or less, but not very enlightening. Meh.
dmckee
@dmckee: Being smartass is my credo. Sometimes it's even considered enlightening (for ex.: http://stackoverflow.com/questions/1483757/1483843#1483843)
Pavel Shved
I think it's a good answer. It answers the question. All that remains is to find a cleaner, more compact and easier to parse bytecode representation.
jalf
@Pavel: Being a smartass doesn't mean not being insightful. +1.
David Thornley
+2  A: 

Yes it is technically feasible. A bit of a plug for a former employer, but here's an implementation of exactly that: http://antixlabs.com/antixGamePlayer/architecture.html. The packaging process is, roughly speaking, C/C++ -> (compiler) -> LLVM -> (backend) -> bespoke bytecode -> zip file. This is platform-independent. Once it's on the user's device the "player" converts bespoke bytecode -> (translator for that device) -> native elf file -> (loader/linker) -> fixed up code.

If the real question is, "does there exist any such industry-standard intermediate format which is widely supported on multiple platforms and suitable for all-purpose use, like Java bytecode?" then the answer is "no".

As for why, I'd say it's because there is no one organisation which has enough influence over C++ programmers, and no true necessity for Java-style deployment of C++ applications. Sun invented Java and a GUI library in one go, presented it to programmers, and didn't introduce the big proliferation of profiles until later.

C++ doesn't even have a standard GUI, and C++ environments are far more fragmented than Java. How do you tell a Windows app developer, a mobile phone developer, a smartcard implementer and a stock exchange backend implementer that they need to ditch their existing toolchain in favour of a platform-independent deployment mechanism for C++? They don't. And that's even before you get to the folks writing OSes and device drivers in C or C++ mixed with assembly. It's simply impossible to come up with a standard environment to support all of them.

Steve Jessop
thanks for detail explanation.+1.
rjoshi