开发者

Difference between C++ and Java compilation process [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Why does C++ compilation take so long?

Hi,

I searched in google for the differences between C++ and Java compilation process, but C++ and Java language features and their differences are returned.

I am proficient in Java, but not in C++. But I fixed few bugs in C++. F开发者_开发问答rom my experience, I noticed that C++ always took more time to build compared to Java for minor changes.

Regards Bala


There are a few high-level differences that come to my mind. Some of those are generalizations and should be prefixed with "Often ..." or "Some compilers ...", but for the sake of readability I'll leave that out.

  • C/C++ compilation doesn't read any information from binary files, but reads method/type definitions only from header files that need to be parsed in full (exception: precompiled headers)
  • C/C++ compilation includes a pre-processor step that can do a wide array of text-replacement (which makes header pre-compilation harder to do)
  • The C++ syntax is a lot more complex than the Java syntax
  • The C++ type system is a lot more complex than the Java type system
  • C++ compilation usually produces native assembler code, which is a lot more complex to produce than the relatively simple byte code
  • C++ compilers need to do optimizations because there isn't any other thing that will do them. The Java compiler pretty much does a simple 1:1 translation of Java source code to Java byte code, no optimizations are done at that step (that's left for the JVM to do).
  • C++ has a template language that's Turing complete! (so strictly speaking C++ code needs to be run to produce executable code and a C++ compiler would need to solve the halting problem to tell you if arbitrary C++ code is compilable).


Java compiles code into bytecode, which is interpreted by the Java VM. C++ must compile into object code, then to machine language. Because of this, it's possible for Java to compile only a single class for minor changes, while C++ object files must be re-linked with other object files to machine code executable (or DLLs). This may make the process take a bit longer.


I am not sure why you expect the compilation speed of Java and C++ to be comparable since they are different languages with completely different design goals and implementations.

That said a few specific differences to keep in mind are:

  • Java is compiled to byte code and not right down to machine code. Compiling to this abstract virtual machine is simpler.
  • C++ compilation involves not only compilation but also linking. So it is typically a multi step process.
  • Java performs some late binding that is the association of a call to a function and the actual code to run is done at runtime. So a small change in one area need not trigger a compile of the whole program. In C++ this association needs to be done at compile time this is called early binding.


A C++ program using all the language's features is inherently more difficult to compile. A few template invocations with a number of types can easily double or triple the amount of code to generate.


Glossing over a lot of details, in Java you compile .java files into one or more .class files. In C++ you compile .cc (or whatever) source files into .o files, and then link the .o files together into an executable or library. The linking process is usually what kills you, especially for minor changes as the amount of work for linking is roughly proportional to the size of your entire project. (this is ignoring incremental linkers, which are specifically designed to not behave as badly for small changes)

Another factor is that the #include mechanism means that whenever you change a .h file, all of the .o files that depend on it need to be rebuilt. In Java, a .class file can depend on more than one .java file (eg: because of constant inlining), but there tend to be far fewer of these "hot spots" where changing one source file requires many other source files to be rebuilt.

Also, if you're using an IDE like Eclipse it's building your Java code in the background all the time, so by the time you tell it to build it's already mostly (if not completely) done.


Java compiles any source code into bytecode, which is interpreted by JVM. Because of this feature it can be used in multiple platform.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜