Compiler#

Remember that we have to translate our source code to machine code, to do this, we need to install a compiler.

compiler

a computer program that translates computer code written in one language into another language

We will use clang compiler which is part of the LLVM project.

Installation#

  1. Go back to the terminal in your editor.

  2. Install it using:

    winget install martinstorsjo.llvm-mingw.ucrt
    

    This command will install the compiler binaries under C:\Users\user\AppData\Local\Microsoft\WinGet\Packages\MartinStorsjo...\llvm-mingw-20...\bin and will put this folder in your path.

    brew install llvm
    
  3. Go back to your editor’s terminal.

  4. Check whether clang is installed and available in our environment by entering the following command in the terminal of the editor:

    clang
    

    You may see a message similar as follows should show up, especially if you are on Windows and macOS:

    clang: error: no input files
    

    If it does not and clang cannot be found, try the following in order:

    1. Probably your package manager modified your environment variable path, but the editor is not aware of this. Restart your editor.

    2. Maybe your package manager did not bother modifying your environment variable path. Refer to section Editing the environment variable path

Usage#

  1. Now compile main.c using the following command:

    clang main.c
    

    You should get no output, which means your source code is compiled to machine code binary a.exe on Windows and a.out in Linux. You should see it in the EXPLORER column.

Running your program#

  1. On the same terminal, type the following to run your program:

    ./a.exe
    
    ./a.out
    

    We need to prefix our program with ./, because your current directory (.) is not part of your path by design.

    You should see no output. The returned number 42 can be seen in the return code:

    $LASTEXITCODE
    
    echo $?
    

    You should see as output:

    42
    

Creating a task for compiling & running our code#

Typing each time commands to the terminal to compile and run our program is not necessary. The editor features tasks, which can be configured to compile our code – or build in general – within the editor.

  1. Try to build your project using CtrlShiftb. You should see No build task ro run found. Configure Build Task....

  2. Click on the last message. You should see Create tasks.json file from template and other menu items related to settings prefixed with the gear icon.

  3. Select Create tasks.json.... You should see different tasks templates including Others | Example to run an arbitrary external command

  4. Click on Others .... .vscode/tasks.json will be created and opened in a new tab. The template defines a task called echo.

  5. Let us try to run the created template. The task echo is not a build task right now, so it cannot be run using the build task.

    To run the build task we could use the traditional menu items on the top. Instead, we will use the command palette, which makes it possible to search for a menu item.

    Press CtrlShiftp. The command palette window should pop up.

  6. Type run task and then select Tasks: Run Task. The window will show many tasks including echo.

  7. Select echo. A new window will show up with Select for which kind of errors ... to scan the task output.

    Scanning the output is helpful, e.g. for:

    1. directly jumping to the error in our source code.

    2. to have an overview of issues in our code on the PROBLEMS tab (left to the TERMINAL tab).

    We are not going to use this feature, as we will directly get feedback about our program syntax mistakes on our editor window through the language server clangd that we will install later.

  8. Click on Continue without scanning the task output. A new terminal window prefixed with 🛠️ echo should be opened and it will output Hello.

    Hello comes from the shell command echo Hello.

    So the template works.

Exercise 1

Convert the template to a task that

  1. compiles our code and then runs it. Use the two commands by chaining them using ;. This means cmd1; cmd2.

  2. is called build & run.

  3. Then run the task.

You should get an output similar to:

 *  Executing task: clang main.c; ./a.exe 


 *  The terminal process "... -Command clang main.c; ./a.exe" terminated with exit code: 1.

Selecting the default build task#

Now we will configure our task as the default build task.

  1. Press CtrlShiftb. You should see No build task to run found. ...

  2. Click on the only item. build & run task should show up.

  3. Select build & run. task.json task will be opened and you will see that tasks.json is augmented with the following lines:

             "problemMatcher": [],
             "group": {
                 "kind": "build",
                 "isDefault": true
             }
    

    These lines:

    1. Deactivate scanning of the output of the task (problemMatcher) (We don’t need it)

    2. Select our task as the default

  4. Now CtrlShiftb again. It should compile and run your program without any prompts.

Tip

The build task automatically saves your project files before running the task. Don’t lose time with saving before executing the task.