Does anyone know of a good Java to C cross compiler?
Do they work well?
very first Java compiler developed by Sun Microsystems was written in C, but now the class libraries are always written in Java (since they are intended to be run using the Java VM itself).
Yes, it is possible for target to be greater than source. That is, we can write code using strictly java 8 syntax, and (using a java 11 or later compiler) compile it to java 11 bytecode.
A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a cross compiler executes on machine X and produces machine code for machine Y.
This is very similar to this question, and answers may be helpful to you: Compiler to translate Java to C.
Summary: There are tools for this (Toba, GCJ, etc), but you may run into problems with not all of the Java libraries being ported. In the end, tools will probably only do PART of the work, and you'll have to hand-code some of the rest.
A good first step is to convert your Java code to only use standard libraries available in Java 1.4. In fact, you'll probably want to wean as much as possible off of anything not in java.lang.* or java.util.* packages in order to simplify the porting procedure.
Depending on the size of your codebase, it may actually be easier to rewrite the bulk directly rather than relying on tools. Java and C have a lot of syntax similarity, but the mismatch between C's straight procedural code, and Java's object oriented features could cause problems. Automated tools may generate virtually unmaintainable C code when trying to work around this, and there's always the possibility for subtle bugs.
2016 update: Don't do this, not now, not ever. The options that used to provide this have not been maintained (GCJ, for example), and it's arguably easier to find a developer fluent in java than C. Also, Java performance has continued to improve, and baseline implementations tend to have similar performance. Optimized C is still faster, but the edge gets smaller and smaller with every JRE version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With