# VSCode C++ Setup Guide This guide will help you setup C++ in VSCode in Windows. It will include a comprehensive explanation on how the C++ source code is compiled, how it is compiled into an executable file, the libraries needed to do compiling and linking, and how to setup your VSCode to run C++ files. Although this article mainly focuses on how to setup C++, it may also be used as a reference to setup C, as those languages go through both the compilation and linking process. Later you will learn that C++ is compiled using a tool called `g++` . To compile C, you can use `gcc` instead. #### What is C++? You might have heard C++ mentioned when you were learning about C. C++ is a superset of C. It has almost all the features C has, but more and it supports "object oriented programming". You can find features such as classes, objects, or strings in C++, but not in C. If you try copy and pasting the code you wrote in a `.c` file into a `.cpp` file, and run it as a C++ program, it will still work as expected (most of the time, with a couple of caveats). #### Why use VSCode? * **Customizability with user-built custom extensions**. Extensions such as live server, intellisense, linters can be extremely helpful during development. * Can be used to code in other languages such as python, javascript, etc. * Dark mode with custom themes * Eye candy. #### Why not to use VSCode? * Fairly complex to setup * Slow intellisense If you just want to run C++ somewhere, you can use online C++ compilers like [OnlineGDB](https://www.onlinegdb.com/online_c++_compiler) # Glossary * **VSCode**: Short for Visual Studio Code. * **Source code**: `.cpp` file in which contains code in C++. * **Object files**: `.obj` or `.o` files. They are files optimized for the computer to read as opposed to the human-readable syntax which can be found in `.cpp` files. You can convert these to assembly code if you're curious how they work in a low-level environment. * **Executable file**: `.exe` file which can be ran by double clicking it in the file explorer. * **Environment variables**: variables used by Windows which can be accessible everywhere. You can read more about environment variables and how to set the `PATH` variable in the internet. (I'm too lazy to find a good source :P). * **C++ Compiler**: Tool used to convert the source code into object files. * **C++ Linker**: Tool used to convert one or more object files into executable files. * **Terminal**: An application that allows you to run commands by typing them out (also called a Command Lin Interface). An example of a terminal you might be familiar with is Windows Command Prompt or Powershell. * **Terminal session**: If you open a powershell window, it will create a "terminal session". If you open another powershell window, it means you have 2 "terminal sessions" running. Some apps allow you to open multiple terminal sessions in a single window (such as VSCode or Windows Terminal). Think of a terminal as a "web browser" while the terminal sessions are the "tabs" you use to browse things. # How C++ Works (In a project workspace) * You write code down which is saved a `.cpp` file. This is called the source code. * Each source code is compiled into object files (.o or .obj files) by the **C++ Compiler** * One or multiple object files are linked by the **C++ Linker** into a single executable binary file (.exe file) * Conclusion: Each `.cpp` file is **compiled** into a `.obj` object file, which are then **linked** into a single `.exe` file <br> Here is a rough idea how a C++ project *might* look like ``` . ├── project │ ├── sourceCode │ │ ├── main.cpp │ │ ├── test.cpp │ │ ├── test2.cpp │ ├── objectFiles │ │ ├── main.obj │ │ ├── test.obj │ │ ├── test2.obj │ ├── main.exe │ └── project.sln // a "solution" file which contains data about the project ``` Note: This is only an example and the project directory layout does not reflect how a real C++ project looks. # Installation Tools for C/C++ Now that we know how the C++ source code needs to be compiled and linked before executed, we need a tool which does the compiling/linking for us. A commonly used tool we can use is called `g++`. #### Common tools for compiling/linking in C/C++ * **g++:** GNU C++ Compiler * **gcc**: GNU C Compiler Both of these tools can be found in a library called MinGW. We'll see how to install MinGW in the guide below. ### MinGW MinGW is a software consisting of tools to compile or run programs such as C++, GoLang, etc. We'll install this to get `g++` and `gcc` in our system. MinGW can be installed by downloading the installer from [this link](https://sourceforge.net/projects/mingw/) or by using a package manager. This guide will be using the `chocolatey` package manager to install chocolatey. ### Chocolatey Chocolatey is a Windows Package Manager, which is a fancy term describing a tool which helps you install programs for windows. Installing multiple software using chocolatey can be as easy as typing in one line in a terminal. Keep in mind that installing applications using choco needs adminitrator permission, so make sure you open your terminal with administrator permissions. Example: `choco install discord spotify vscode steam grammarly -y` # Installing Chocolatey, MinGW, and VSCode * Open your Powershell terminal with **administrator permissions** * Type in the following line to install chocolatey: ```powershell= Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) ``` * install MinGW and VSCode by running `choco install mingw vscode -y` # Running C++ ### Creating the C++ file Create a file named `main.cpp` anywhere in your system and copy and paste the following code into the file. You can open the file using notepad or other text editors. ```cpp #include <iostream> using namespace std; int main() { cout << "Hello World" << endl; return 0; } ``` ### Running it in VSCode * First you need to install an extension for VSCode to understand what C++ is. Press <kbd>ctrl</kbd>+<kbd>shift</kbd>+<kbd>x</kbd> to open the extensions tab. Next search for the `C/C++` by Microsoft and install it. * Open the directory where your `main.cpp` is located. You can go to `File > Open Folder` to do this. ![](https://cdn.discordapp.com/attachments/1019580811130966017/1020944204098195486/unknown.png) * Open or toggle the terminal by pressing <kbd>ctrl</kbd>+<kbd>\`</kbd> (backtick). ![](https://cdn.discordapp.com/attachments/1019580811130966017/1020944352744312842/unknown.png) * Run the code by typing in `g++ -o main.exe .\main.cpp ; .\main.exe` ![](https://cdn.discordapp.com/attachments/1019580811130966017/1020944595095408680/unknown.png) ### Setting up Run shortcut If typing that in is still too long for your convenience, you can use the <kbd>ctrl</kbd>+<kbd>F5</kbd> shortcut. But to do that you will need to set up a couple of things first: * Press <kbd>ctrl</kbd>+<kbd>F5</kbd> and this will pop up ![](https://cdn.discordapp.com/attachments/1019580811130966017/1020943253828280380/unknown.png) Choose the `C++ (GDB/LLDB)` debugger. GDB is a short for GNU Program Debugger, another program inside MinGW installed along g++ and gcc for debugging programs. * Next it will prompt you to select a configuration ![](https://cdn.discordapp.com/attachments/1019580811130966017/1021019212854067270/unknown.png) Choose the one with `chocolatey\lib\mingw\tools\install\mingw64\bin\g++.exe`. * Press <kbd>ctrl</kbd>+<kbd>F5</kbd> again and it run the program. ![](https://cdn.discordapp.com/attachments/1019580811130966017/1020946698224279602/unknown.png) * Now every time you make changes to the file, just save the file and press the shortcut. It will run the program and output it to the `TERMINAL` tab. # Extra: Running C++ using Powershell This method will guide you through compiling, linking, and running C++ step by step using g++. ### Compiling, Linking, and Running the file with Powershell 1. Open Powershell ![](https://cdn.discordapp.com/attachments/1019580811130966017/1020935463227367434/unknown.png) 2. Navigate to the `main.cpp` file using the `cd` command. You can learn more about how to use the `cd` command [here](https://www.youtube.com/watch?v=cTIilDTaTs0) ![](https://cdn.discordapp.com/attachments/1019580811130966017/1020936426524774471/unknown.png) * Compile the source code into object files (.obj file) using the following command: `g++ -c main.cpp` This will produce a `main.o` file in the same directory. * Link the object files into an executable file (.exe file) using the following command `g++ -o mainExecutable main.o` This will create a `mainExecutable.exe` * Run the file using `./mainExecutable.exe` ![](https://cdn.discordapp.com/attachments/1019580811130966017/1020938255077748807/unknown.png) ### Short command for compiling a single file If all that is too long, you can use this command to compile, link, and run a single file. Note that it creates an executable file but **not** an object file. * Command to compile and run C++ (Powershell): `g++ -o main.exe .\main.cpp ; .\main.exe` Summarized by <img src="https://cdn.discordapp.com/attachments/795800794725220402/1077566813824614430/image.png" style="height: 30px; border-radius: 50%;"/> **Stevenn#1001**