java C++和Java编译过程的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2095277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between C++ and Java compilation process
提问by Boolean
Possible Duplicate:
Why does C++ compilation take so long?
可能的重复:
为什么 C++ 编译需要这么长时间?
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.
我在google上搜索了C++和Java编译过程的区别,但是返回的是C++和Java语言的特性和区别。
I am proficient in Java, but not in C++. But I fixed few bugs in C++. From my experience, I noticed that C++ always took more time to build compared to Java for minor changes.
我精通Java,但不精通C++。但是我修复了 C++ 中的一些错误。根据我的经验,我注意到 C++ 总是比 Java 花费更多的时间来构建微小的更改。
Regards Bala
问候巴拉
采纳答案by Joachim Sauer
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 runto produce executable code and a C++ compiler would need to solve the halting problem to tell you if arbitrary C++ code is compilable).
- C/C++ 编译不从二进制文件中读取任何信息,而是仅从需要完整解析的头文件中读取方法/类型定义(例外:预编译头)
- C/C++ 编译包括一个预处理器步骤,可以进行大量的文本替换(这使得头预编译更难做到)
- C++ 语法比 Java 语法复杂得多
- C++ 类型系统比 Java 类型系统复杂得多
- C++ 编译通常会生成本地汇编代码,与相对简单的字节码相比,生成的代码要复杂得多
- C++ 编译器需要做优化,因为没有任何其他事情可以做它们。Java 编译器几乎将 Java 源代码简单地 1:1 转换为 Java 字节码,在该步骤中没有进行任何优化(由 JVM 完成)。
- C++ 有一种图灵完备的模板语言!(所以严格来说,需要运行C++ 代码才能生成可执行代码,而 C++ 编译器需要解决停机问题来告诉您是否可以编译任意 C++ 代码)。
回答by Aaron
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.
Java 将代码编译成字节码,由 Java VM 进行解释。C++ 必须编译成目标代码,然后编译成机器语言。正因为如此,Java 可能只编译一个类以进行微小的更改,而 C++ 目标文件必须与其他目标文件重新链接到机器代码可执行文件(或 DLL)。这可能会使该过程需要更长的时间。
回答by Tendayi Mawushe
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.
我不知道为什么您期望 Java 和 C++ 的编译速度具有可比性,因为它们是不同的语言,具有完全不同的设计目标和实现。
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.
回答by Dusk
Java compiles any source code into bytecode, which is interpreted by JVM. Because of this feature it can be used in multiple platform.
Java 将任何源代码编译成字节码,由 JVM 解释。由于此功能,它可以在多个平台中使用。
回答by wallyk
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.
使用所有语言特性的 C++ 程序天生就更难编译。一些具有多种类型的模板调用可以轻松地将要生成的代码量增加一倍或三倍。
回答by Laurence Gonsalves
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)
忽略很多细节,在 Java 中,您将 .java 文件编译成一个或多个 .class 文件。在 C++ 中,您将 .cc(或其他)源文件编译为 .o 文件,然后将 .o 文件链接到一个可执行文件或库中。链接过程通常会杀死您,尤其是对于微小的更改,因为链接的工作量大致与整个项目的大小成正比。(这忽略了增量链接器,这些链接器专门设计为不会因为小的变化而表现得那么糟糕)
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.
另一个因素是#include 机制意味着每当您更改 .h 文件时,所有依赖于它的 .o 文件都需要重新构建。在 Java 中,一个 .class 文件可以依赖于多个 .java 文件(例如:因为不断内联),但是这些“热点”往往要少得多,其中更改一个源文件需要许多其他源文件重建。
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.
此外,如果您使用 Eclipse 这样的 IDE,它会一直在后台构建您的 Java 代码,因此当您告诉它构建时,它已经大部分(如果不是完全)完成。

