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#
Go back to the terminal in your editor.
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
Go back to your editor’s terminal.
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:Probably your package manager modified your environment variable
path
, but the editor is not aware of this. Restart your editor.Maybe your package manager did not bother modifying your environment variable
path
. Refer to section Editing the environment variable path
Usage#
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 anda.out
in Linux. You should see it in theEXPLORER
column.
Running your program#
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 yourpath
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.
Try to build your project using CtrlShiftb. You should see
No build task ro run found. Configure Build Task...
.Click on the last message. You should see
Create tasks.json file from template
and other menu items related to settings prefixed with thegear icon.
Select
Create tasks.json...
. You should see different tasks templates includingOthers | Example to run an arbitrary external command
Click on
Others ...
..vscode/tasks.json
will be created and opened in a new tab. The template defines a task calledecho
.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.
Type
run task
and then selectTasks: Run Task
. The window will show many tasks includingecho
.Select
echo
. A new window will show up withSelect for which kind of errors ... to scan the task output
.Scanning the output is helpful, e.g. for:
directly jumping to the error in our source code.
to have an overview of issues in our code on the
PROBLEMS
tab (left to theTERMINAL
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.Click on
Continue without scanning the task output
. A new terminal window prefixed with 🛠️ echo should be opened and it will outputHello
.Hello
comes from the shell commandecho Hello
.So the template works.
Exercise 1
Convert the template to a task that
compiles our code and then runs it. Use the two commands by chaining them using
;
. This meanscmd1; cmd2
.is called
build & run
.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.
Press CtrlShiftb. You should see
No build task to run found. ...
Click on the only item.
build & run
task should show up.Select
build & run
.task.json
task will be opened and you will see thattasks.json
is augmented with the following lines:"problemMatcher": [], "group": { "kind": "build", "isDefault": true }
These lines:
Deactivate scanning of the output of the task (
problemMatcher
) (We don’t need it)Select our task as the default
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.