After reading LLVM and LLVM-Intro, now it’s clear for me that how the LLVM works for C++.

##1, How to generate our codes to target instruction set in LLVM?

In an LLVM-based compiler, there are three phrase to it:

  1. Frontend Phrase: it is responsible for parsing, validating and diagnosing errors in the input code, then translating the parsed code into LLVM IR (usually, but not always, by building an AST and then converting the AST to LLVM IR).
  2. Optimization Phrase: The IR is optionally fed through a series of analysis and optimization passes which improve the code.
  3. Backend Phrase: maps the code onto the target instruction set(code generator).

##2, The front-end in LLVM for building C++

We can use clang or llvm-gcc-4.2 to compile C++ in “XCode 4.x”. However, Apple has removed llvm-gcc-4.2 in “XCode 5”, because clang is much better than llvm-gcc-4.2:

clang is responsible for translating code into LLVM IR, we can find it at “Build Option” in xcode’s project configuration:

##3, The optimization and back-end in LLVM

As we saw, the “LLVM Optimizer” will optimize the LLVM IR to ”*.o”. And “LLVM Linker” will analyze and combine the LLVM IR together. After an optimization of the linked IR, the “LLVM Backend” will generate them to target machine code. Finally, the ”*.out” we got can be run on users’ machine.

##4, The different between “libstdc++” and “libc++”

Both the libstdc++ and libc++ are the C++ standard library:

  1. The libstdc++ is developed and released as part of GCC.
  2. The libc++ is a new implementation of the C++ standard library, targeting C++11.

Accroding to the document of libc++, it is well supported to C++11, faster execution, minimal memory use, faster compile times than libstdc++, but it’s only known to work on “Mac OSX”. However, we can take risks to use libc++ on iOS. Since libstdc++ has many years’ experiences, it is the default value of “C++ Standard Library” in XCode:

We must also include the “libc++.dylib” while using libc++, or “libstdc++.dylib” while using libstdc++:

There will be link errors if we choose the wrong dylib of “C++ Standard Library”(e.g we choose “libc++.dylib” but we choose libstdc++ in “C++ Standrad Library”):



评论需要翻墙 for disqus