Lab 1 - A - Programming-Environment
Lab 1 - A - Programming-Environment
During the semester the compiler that is going to be used is gcc. It compiles C/C++
source code into executables.
Although it can be executed from the command line it is advantageous to use an IDE
(Integrated Development Environment).
The IDE seamless integrates the writing of the code the compilation, error finding,
debugging and execution all inside a single environment.
In the case of Linux and Mac OS X, VSCode uses the installed compilers.
In Windows it can use the Windows Subsystem for Linux (that replicates a Linux OS
inside Windows).
In the middle of this document there is two small exercises for you to solve.
Table of Contents
1 Install Visual Studio Code............................................................................................. 3
2.1 Linux...................................................................................................................... 4
2.2 Windows................................................................................................................ 4
3 Debug C programs........................................................................................................6
4 Exercise 1..................................................................................................................... 7
5 Use of makefiles............................................................................................................8
5.2 Makefile..................................................................................................................9
6 Exercise 2....................................................................................................................12
• https://code.visualstudio.com/docs/setup/setup-overview
This simple tutorial guides the installation on the three different operating systems:
Linux, MAC OS X and Windows
After installation, students should follow the GET STARTED guided tour to get
acquainted with VSCode:
• https://code.visualstudio.com/docs/getstarted/introvideos
2 Install C++ Extensions
In order to compile C programs it is necessary to install the C++ extension.
2.1 Linux
If the student is using Linux, it is necessary to install the gcc compiler and the necessary
libraries. In Ubuntu this is done with the command:
• https://code.visualstudio.com/docs/cpp/config-linux
After installing all the dependencies guarantee that everything is running smoothly by
executing the example presented in the previous guides.
This example is in C++, but if you can run it, you can run C programs also.
2.2 Windows
https://learn.microsoft.com/pt-pt/windows/wsl/install
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl
https://code.visualstudio.com/docs/cpp/config-wsl
In Windows it is necessary to install:
◦ https://learn.microsoft.com/pt-pt/windows/wsl/install
◦ install and configure one Linux distribution inside the WSL (for instance
Ubuntu)
◦ https://code.visualstudio.com/docs/cpp/config-wsl
• https://code.visualstudio.com/docs/cpp/config-wsl
After installing all the dependencies guarantee that everything is running smoothly by
executing the example presented in the previous guides.
This example is in C++, but if you can run it, you can run C programs also.
2.3 MAC OS X
https://code.visualstudio.com/docs/cpp/config-clang-mac
In MAC OS X it is necessary to guarantee that you have the Xcode installed
• https://code.visualstudio.com/docs/cpp/config-clang-mac
After installing all the dependencies guarantee that everything is running smoothly by
executing the example presented in the previous guides.
This example is in C++, but if you can run it, you can run C programs also.
https://learn.microsoft.com/pt-pt/windows/wsl/install
https://code.visualstudio.com/docs/cpp/config-wsl
https://code.visualstudio.com/docs/cpp/config-clang-mac
3 Debug C programs
https://code.visualstudio.com/docs/cpp/cpp-debug
The VS Code allows debugging of C program. It allows the regular execution of
applications with the added functionalities:
• it is possible to execute python expressions that will change the state of the
applications
• https://code.visualstudio.com/docs/cpp/cpp-debug
4 Exercise 1
In this exercise students will use VS Code to edit, compile and run a program.
◦ you can create a file called lab1.c and copy to it the C code
Why does the random function always return the same number?
Is random() really random?
The Visual Studio Code can use a standard makefiles to help compile a project and run
applications in a project. To accomplish this, it is necessary first install an extension,
then write the suitable makefile and finally configure the extension.
https://marketplace.visualstudio.com/items?itemName=ms-
vscode.makefile-tools
Select the Extensions tab and search for the Makefile Tools from Microsoft
Extensions tab
To ease the development the following required rules should also be defined:
• all
• clean
In order to allow the debug of the applications, it is necessary to use the -g compilation
flag.
Later, when the user clicks on the compile button, the Make command will try to execute
the selected rule (in this case compile the main application.
To run the application inside VSCode or debug it it is also necessary to configure the
Launch target.
Whe compiling it is possible to see the compilation/linking errors and correct them.
To debug the target program, students should define the suitable breakpoints and run
the program in the debugger.
The Visual Studio Code offers a set of tools that allow multiple user to interact in the
same workspace and files.
This allows remote users (in different computers) to control the same workspace:
• All users can execute the applications and interact with them
To activate this functionality it is necessary to install the Visual studio live share.
• https://docs.microsoft.com/en-us/visualstudio/liveshare/use/vscode
You will be required to sign in. You can use your TEAMS/Técnico account to do so.
After sharing a session, the remote partner can access your work from his installed
VSCode or even from the browser:
• https://docs.microsoft.com/en-us/visualstudio/liveshare/quickstart/join
• https://docs.microsoft.com/en-us/visualstudio/liveshare/quickstart/browser-join
Now all participants can cooperate on the same workspace in real time.