If clang is the front end of a compiler, then why it can produce executable file?

If you are very specific: the clang executable is a compiler driver. It invokes all parts needed to produce an executable. It invokes libclang which does the front-end jobs: parser/lexer, the semantic analysis, building the AST and code generation. When the AST is lowered to LLVM IR the front-end jobs are done and the optimizer and LLVM kicks in. After optmizing the code the compiler driver will invoke the LLVM back-end specified by the target and finally the linker which builds the executable. And that is why the clang compiler driver can build executables.


LLVM is a compiler backend that was written before clang, which originally used the front end from gcc in a tool called 'llvm-gcc'. Clang is the name of the front end code, but clang is also the name of a tool that includes the clang front end, but will also run the whole compile for you. Later phases of compilation are either built into the clang tool as libraries, or if they are separate executables clang knows how to invoke them. With the right command line arguments, you can make clang stop part way thru

  • -emit-ast just does the parse and makes the Abstract Syntax Tree
  • -emit-llvm makes the LLVM Intermediate Representation, but not turn it into code for your computer

Clang will work as the driver for the whole build because that's what programmers usually want, the soure parsed, the object generated, the executable made. Wanting the Abstract Syntax Tree spit back at you is pretty rare.

Obviously this is the souce for all thing LLVM http://llvm.org

Here is a video of Chriss Lattner explaing what LLVM is https://www.youtube.com/watch?v=029YXzHtRy0 . Chandler Carruth has some vids on youtuble explain parts of clang that he has worked on.