48

When compiling C++ you of course use a compiler for the target platform you want to compile for. Is there a C++ compiler that targets the JVM (so instead of using the Java "native" interface C++ code is compiled to Java byte code)?

knpwrs
  • 15,691
  • 12
  • 62
  • 103

2 Answers2

36

NestedVM provides binary translation for Java Bytecode. This is done by having GCC compile to a MIPS binary which is then translated to a Java class file. Hence any application written in C, C++, Fortran, or any other language supported by GCC can be run in 100% pure Java with no source changes.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 1
    Why on earth would you want to run a C or C++ program in the Java VM? – TheLQ Nov 19 '10 at 02:46
  • 4
    @TheLQ Maybe the OP was envious of C++/CLI? – chrisaycock Nov 19 '10 at 02:57
  • I actually found this independently (I remembered something about GCC MIPS, and so I Googled "GCC MIPS JVM"). But in retrospect it's not surprising that it's already been in an answer on SO. – ephemient Nov 19 '10 at 04:14
  • 3
    Really, the reason I was interested is that Mathematica 8 can now generate C from Mathematica code. I was interested in running code made in Mathematica inside of a Java program without using MathLink or some other technology that would require the end user to install extra software. Really, this whole idea is only conceptual for me. Source: http://awurl.com/FTlvm62ns – knpwrs Nov 19 '10 at 04:15
  • 2
    @TheLQ: `Why on earth would you want to run a C or C++ program in the Java VM?`. Well, for C++, having fully featured RAII on a JVM would be a good thing. And operator overloading. And, well, lambdas... :-D – paercebal Dec 16 '12 at 22:36
  • And simply because Standard C++ code is portable on a host of platforms, including the CLR. (I'm not talking about libraries) The JVM seems to be Terra Incognita on that matter. @chrisaycock: I'm not the OP but yes, I'm envious of C++/CLI :) – Laurent LA RIZZA Jul 03 '13 at 11:26
  • @ TheLQ the simple answer is that any working C or C++ code can be used on the JVM. Porting, rewrites, etc take time and money. A compiler with some tests is a better option in many cases. I could see it being useful with some legacy code issues too. – Nick P Jul 31 '13 at 20:28
  • @TheLQ: "Why on earth would you want to run a C or C++ program in the Java VM?": For you I recommend think about "if all you have is a hammer, everything looks like a nail"; The answer is same reason why C/C++ is implemented for CLI,,,,, – Abdurrahim Dec 25 '16 at 14:21
13

As I have answerd on this similar SO question there is a new kid in town called gcc-bridge. It's a maven plugin and supports all the gnu laguages like c, c++, fortran.

Community
  • 1
  • 1
KIC
  • 5,887
  • 7
  • 58
  • 98