Search code examples
compiler-constructionclangllvm

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


One thing I really don't understand is about the function of clang, if clang is the front end part of the compiler, it should just do the parser work for the source code, the the remain work will be done by LLVM. But clang can produce executable file too. So how to understand it ?What is the relation between clang and llvm?


Solution

  • 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.