Lab 1 Manual
How to setup C++ on Windows
We will install two things:
1- A Compiler
2- A Code Editor
What is a Compiler?
Computers don’t understand English or any higher programming language like C++,
they only understand the language of zeros and ones (0101…) i.e. binary. Therefore,
we need a translator to translate our C++ code into machine-readable binary code.
This is the job of a compiler. We will use the MinGW compiler. It will convert your
.cpp file (which contains your C++ code) into an executable .exe file that you can run
on your CPU.
What is a Code Editor?
A Code Editor helps us write code more efficiently and in a way that helps us avoid
bugs, for instance, through syntax highlighting etc. We will be using Microsoft
Visual Studio Code as our code editor.
Note: You have to install “Visual Studio Code”, not “Visual Studio”. These are
different programs.
Steps to install MinGW Compiler:
1. Go to this website https://sourceforge.net/projects/mingw/ and download
MinGW
2. Run the downloaded setup and press install
3. Change installation directory if you want, check “…for all users * …” box if
you want. Press continue
4. When the installation is completed, press continue, and MinGW
Installation Manager will open
5. Right-click on each entry under the Package heading and click “Mark for
Installation”
6. Press “Installation” on the top left and click on “Apply Changes”
7. A window will open, press “Apply” again
8. After the installation is complete press “Close”
Note: If on pressing “Close” the setup says that packages will be discarded
then do step 6 again because some files were not downloaded.
9. Go to the directory where you installed MinGW and open “bin” folder
10. Now click on the top bar in the folder (Anywhere after bin)
11. Now copy the directory
12. Search “Environment variables” and open “Edit the system environment
variables”
13. A window will open, click on “Environment Variables”
14. Another window will open. On this window under the “System Variables”,
select “Path” and then press “Edit”
15. Another window will open. Click new and paste the copied directory and
press “Ok”
16. Now search cmd and open Command Prompt
17. Now to test if MinGW compiler was successfully installed type “gcc -v”
without the quotation marks in Command Prompt and press enter. If you
see “gcc is not recognized as an internal or external command” as the
output, then you have done something wrong while setting up the
environment variable
Steps to install Visual Studio Code:
1. Go to https://code.visualstudio.com/download this link and download
for windows then open the downloaded setup
2. Accept the agreement and press “Next”
3. Select the directory where you want to install and press “Next”
4. Again press “Next”
5. Now check all the boxes MOST IMPORTANTLY the “Add to Path” box
and press “Next”
6. Now press “Install”
7. After the installation is complete check “Launch Visual Studio Code”
box and click on “Finish”
8. VSCode will open, now click on “Extensions” in the left bar
9. Search “C++” and click on the first search result (the result shown in
the image” and press “Install”
How to run C++ programs:
1. In VSCode go to “File” and click on “New File” and an editable text file will
open
2. Now again go to “File” and click on “Save as”
3. Now rename the file to <anything>.cpp e.g., in my example I have named
it Hello.cpp. DON’T FORGET TO ADD .cpp AT THE END and then press
save
4. Now time to write a few lines of code!
5. Now you need to open Command Prompt and open the directory where
your cpp file is stored. The easiest way to do this is to go to the folder
where cpp file is located and then click on the top bar and type “cmd” and
press enter
6. Now type “g++ Hello.cpp -o Hello & Hello” and press enter.
g++ <FileName>.cpp -o <AnyName> & <TheNameUsed>
g++ Hello.cpp -o Hello will make an .exe file of name Hello, the & is used
when you are giving another command e.g. command 1 & command 2 here
command 1 will be executed and then command 2 will be executed. In our
case .exe file is generated and then our second command, Hello will give us
the output
How to setup C++ on Mac
Steps to install and Setup Visual Studio Code for C++:
1. Go to https://code.visualstudio.com/download this link and download for Mac
then open the downloaded setup. It’s a 169MB file so might take few minutes
to download. Patience is the key!
2. Drag the zip from downloads to Desktop. Double click on it.
3. Double click on the VS Code’s icon and click open.
4. VS Code will open, now click on “Extensions” in the left bar
5. Search “C++” and click on the first search result (the result shown in the image)
and press “Install”
6. Search “Code Runner” and click on the first search result (the result shown in the
image) and press “Install”
7. Now to open the terminal, click launchpad. Open others and now click on terminal.
8. Clang may already be installed on your Mac. To verify that it is, open a macOS
Terminal window and enter the following command: ‘clang –version’
9. If Clang isn't installed, enter the following command on terminal:
xcode-select –install
10. Press install
11. Agree to terms and conditions:
12. Click “File” and then “New File” or command N and a new file will open
13. Click “File” and then “Save As” or command S and save the file in the directory
of your choice. While naming the file it is very important to add “.cpp” extension at
end of the filename!!! Or else the compiler wouldn’t know this is a C++ file L
14. Now time to write a few lines of code!
Write the code, then save using command S and then run the code.
The output will appear.
Excellent Job!!!