Compiling & Executing C++ Programs
Using Visual Studio Code
Lecture Notes
1. Lecture Overview
● Objective: Learn how to compile and run C++ programs using Visual Studio Code
(VS Code).
● Prerequisites:
○ VS Code installed
○ C++ extension installed in VS Code
○ MinGW compiler installed
● Note: Compilation converts source code (human-readable) → machine code
(executable).
2. Creating a C++ Source File in VS Code
Steps:
1. Create a folder in any location (e.g., Desktop).
○ Example folder: VSSample
2. Open Visual Studio Code.
3. Go to File → Open Folder, select the folder you created (VSSample).
4. Click New File button → enter a file name with .cpp extension.
○ Example: [Link]
○ VS Code recognizes it as a C++ file (icon changes).
5. Type or paste your C++ code in the editor.
○ Example program output: "Welcome to the C++ world"
6. Save the file: Ctrl + S or click the save button.
3. Compiling & Executing the Program in VS Code
There are two methods to compile and run programs in VS Code:
Method 1: Using Run Build Task
1. Go to Terminal → Run Build Task.
2. Select: C/C++: g++.exe build active file
3. VS Code will compile the program and generate an executable:
○ Example: [Link] (same as source file name)
4. To run the program:
○ Open Terminal → New Terminal
○ Type:
./[Link]
5.
○ Output:
Welcome to the C++ world
6.
Notes:
● .exe file name is automatically set to the source file name.
● In VS Code terminal, always use ./ before the executable name.
Method 2: Using Terminal Commands (Like CMD)
1. Open Terminal → New Terminal in VS Code.
2. Ensure the path points to the folder containing your source file.
3. Compile using one of the following commands:
Default Output File
g++ [Link]
● Example:
g++ [Link]
● Output: [Link] (default executable)
● Run it:
./[Link]
Custom Output File Name
g++ [Link] -o output_filename
● Example:
g++ [Link] -o myoutput
● Output: [Link]
● Run it:
./[Link]
Notes:
● In VS Code terminal, always use ./ before executable names.
● Behavior is almost identical to CMD, except for the ./ prefix.
4. Summary of Commands in VS Code Terminal
Task Command
Compile default output g++ [Link]
Compile with custom output g++ [Link] -o
output_filename
Run executable ./output_filename.exe
5. Key Points to Remember
● Compilation is mandatory before running programs.
● VS Code generates the executable in the same folder as the source file.
● Two ways to compile: Run Build Task or terminal commands.
● Always prepend ./ when executing the program in VS Code terminal.
● Custom output file names can be specified using -o.
End of Lecture Notes