C++ OS X 上的 LLVM 与 clang

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5708610/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:42:12  来源:igfitidea点击:

LLVM vs clang on OS X

c++cgccllvmclang

提问by dominik

I have a question concerning llvm, clang, and gcc on OS X.

我有一个关于 OS X 上的 llvm、clang 和 gcc 的问题。

What is the difference between the llvm-gcc 4.2, llvm 2.0 and clang? I know that they all build on llvm but how are they different?

llvm-gcc 4.2、llvm 2.0 和 clang 有什么区别?我知道它们都建立在 llvm 上,但它们有何不同?

Besides faster compiling, what is the advantage of llvm over gcc?

除了更快的编译之外,llvm 相对于 gcc 的优势是什么?

回答by Brian Campbell

LLVM originally stood for "low-level virtual machine", though it now just stands for itself as it has grown to be something other than a traditional virtual machine. It is a set of libraries and tools, as well as a standardized intermediate representation, that can be used to help build compilers and just-in-time compilers. It cannot compile anything other than its own intermediate representation on its own; it needs a language-specific frontend in order to do so. If people just refer to LLVM, they probably mean just the low-level library and tools. Some people might refer to Clang or llvm-gcc incorrectly as "LLVM", which may cause some confusion.

LLVM 最初代表“低级虚拟机”,但现在它只代表自己,因为它已经发展成为不同于传统虚拟机的东西。它是一组库和工具以及标准化的中间表示,可用于帮助构建编译器和即时编译器。除了自己的中间表示之外,它不能编译任何东西;为此,它需要一个特定于语言的前端。如果人们只是指 LLVM,他们可能只是指低级库和工具。有些人可能会错误地将 Clang 或 llvm-gcc 称为“LLVM”,这可能会引起一些混淆。

llvm-gcc is a modified version of GCC, which uses LLVM as its backend instead of GCC's own. It is now deprecated, in favor of DragonEgg, which uses GCC's new plugin system to do the same thing without forking GCC.

llvm-gcc 是 GCC 的修改版本,它使用 LLVM 作为后端,而不是 GCC 自己的后端。它现在已被弃用,取而代之的是 DragonEgg,它使用 GCC 的新插件系统来做同样的事情而无需分叉 GCC。

Clang is a whole new C/C++/Objective-C compiler, which uses its own frontend, and LLVM as the backend. The advantages it provides are better error messages, faster compile time, and an easier way for other tools to hook into the compilation process (like the LLDB debuggerand Clang static analyzer). It's also reasonably modular, and so can be used as a library for other software that needs to analyze C, C++, or Objective-C code.

Clang 是一个全新的 C/C++/Objective-C 编译器,它使用自己的前端,LLVM 作为后端。它提供的优点是更好的错误消息、更快的编译时间,以及其他工具更容易挂接到编译过程的方法(如LLDB 调试器Clang 静态分析器)。它也是相当模块化的,因此可以用作其他需要分析 C、C++ 或 Objective-C 代码的软件的库。

Each of these approaches (plain GCC, GCC + LLVM, and Clang) have their advantages and disadvantages. The last few sets of benchmarks I've seen showed GCC to produce slightly faster code in most test cases (though LLVM had a slight edge in a few), while LLVM and Clang gave significantly better compile times. GCC and the GCC/LLVM combos have the advantage that a lot more code has been tested and works on the GCC flavor of C; there are some compiler specific extensions that only GCC has, and some places where the standard allows the implementation to vary but code depends on one particular implementation. It is a lot more likely if you get a large amount of legacy C code that it will work in GCC than that it will work in Clang, though this is improving over time.

这些方法(普通 GCC、GCC + LLVM 和 Clang)中的每一种都有其优点和缺点。我看到的最后几组基准测试表明,GCC 在大多数测试用例中生成的代码速度稍快(尽管 LLVM 在少数情况下略有优势),而 LLVM 和 Clang 的编译时间明显更好。GCC 和 GCC/LLVM 组合的优势在于,已经测试了更多代码并在 C 的 GCC 风格上运行;有一些编译器特定的扩展,只有 GCC 才有,有些地方标准允许实现变化,但代码取决于一个特定的实现。如果您获得大量遗留 C 代码,它在 GCC 中的工作比在 Clang 中工作的可能性要大得多,尽管随着时间的推移,这种情况正在改善。

回答by Matthieu M.

There are 2 different things here.

这里有两种不同的东西。

LLVM is a backend compiler meant to build compilers on top of it. It deals with optimizations and production of code adapted to the target architecture.

LLVM 是一个后端编译器,旨在在它之上构建编译器。它处理适应目标架构的代码的优化和生产。

CLang is a front end which parses C, C++ and Objective C code and translates it into a representation suitable for LLVM.

CLang 是一个前端,它解析 C、C++ 和 Objective C 代码并将其转换为适合 LLVM 的表示。

llvm gcc was an initial version of a llvm based C++ compiler based on gcc 4.2, which is now deprecated since CLang can parse everything it could parse, and more.

llvm gcc 是基于 gcc 4.2 的基于 llvm 的 C++ 编译器的初始版本,现在已弃用,因为 CLang 可以解析它可以解析的所有内容,等等。

Finally, the main difference between CLang and gcc does not lie in the produced code but in the approach. While gcc is monolithic, CLang has been built as a suite of libraries. This modular design allow great reuse opportunities for IDE or completion tools for example.

最后,CLang 和 gcc 之间的主要区别不在于生成的代码,而在于方法。虽然 gcc 是单一的,但 CLang 已构建为一套库。例如,这种模块化设计为 IDE 或完成工具提供了很好的重用机会。

At the moment, the code produced by gcc 4.6 is generally a bit faster, but CLang is closing the gap.

目前,gcc 4.6 生成的代码通常要快一些,但 CLang 正在缩小差距。

回答by Stephen Canon

llvm-gcc-4.2 uses the GCC front-end to parse your code, then generates the compiled output using LLVM.

llvm-gcc-4.2 使用 GCC 前端来解析您的代码,然后使用 LLVM 生成编译输出。

The "llvm compiler 2.0" uses the clang front-end to parse your code, and generates the compiled output using LLVM. "clang" is actually just the name for this front-end, but it is often used casually as a name for the compiler as a whole.

“llvm compiler 2.0”使用clang前端来解析你的代码,并使用LLVM生成编译后的输出。“clang”实际上只是这个前端的名称,但它经常被随意用作整个编译器的名称。