Using the C frontend Clang

In this recipe, you will get to know how the Clang frontend can be used for different purposes.

Getting ready

You will need Clang tool.

How to do it…

Clang can be used as the high-level compiler driver. Let us show it using an example:

  1. Create a hello world C code, test.c:
    $ cat test.c
    #include<stdio.h>
    int main() {
    printf("hello world\n");
    return 0; }
    
  2. Use Clang as a compiler driver to generate the executable a.out file, which on execution gives the output as expected:
    $ clang test.c
    $ ./a.out
    hello world
    

    Here the test.c file containing C code is created. Using Clang we compile it and produce an executable that on execution gives the desired result.

  3. Clang can be used in preprocessor only mode by providing the –E flag. In the ...

Get LLVM Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.