Running C++ code in Visual Studio Code (VS Code) requires a few steps to set up your environment. Here's a guide to get you started:
Step 1: Install Visual Studio Code
If you haven't already, download and install Visual Studio Code from the official website.
Step 2: Install the C++ Extension
Open VS Code.
Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side or pressing
Ctrl+Shift+X
.Search for C/C++ and install the extension provided by Microsoft.
Step 3: Install a C++ Compiler
You need a C++ compiler to build and run your code. Common compilers include:
GCC (GNU Compiler Collection): Available on Linux and macOS by default, or via package managers like MinGW-w64 for Windows.
Microsoft Visual C++: Comes with Visual Studio.
For Windows:
Install MinGW-w64:
Download MinGW-w64 from the MinGW-w64 SourceForge page.
Run the installer and follow the instructions to set it up.
Add the
bin
directory of your MinGW installation (e.g.,C:\Program Files\mingw-w64\...\mingw64\bin
) to the system PATH.
Step 4: Configure Your Build Environment
Create a new folder for your C++ project and open it in VS Code.
Create a new file with a
.cpp
extension, likemain.cpp
.Write your C++ code in this file.
Step 5: Set Up a Task to Compile C++
VS Code uses tasks to run build commands. You'll need to create a tasks.json
file to automate the build process.
Press
Ctrl+Shift+B
to open the build tasks dialog.If prompted, select Create tasks.json from template.
Choose Others or C/C++ depending on the prompt.
Modify the
tasks.json
file to include your specific compiler command. Here’s an example:
jsonCopy code{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Generated task by VS Code."
}
]
}
Step 6: Run the C++ Code
Press
Ctrl+Shift+B
to build your program. If everything is configured correctly, it should compile without errors.If the build is successful, you can run the program:
Windows: Open a terminal in VS Code and type
./filename.exe
.Linux/macOS: Type
./filename.out
or./filename
depending on your compiler settings.
Step 7: (Optional) Set Up a Launch Configuration
You can configure VS Code to automatically run the compiled executable after building.
Go to the Run view by clicking the play icon or pressing
Ctrl+Shift+D
.Click on create a launch.json file.
Select C++ (GDB/LLDB) or C++ (Windows) based on your environment.
Modify the generated
launch.json
file to point to the compiled executable.
Example for a simple C++ file:
jsonCopy code{
"version": "0.2.0",
"configurations": [
{
"name": "Run C++",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [],
"externalConsole": true
}
]
}
Step 8: Debugging (Optional)
If you want to debug your C++ code:
Set breakpoints by clicking on the left margin next to the line numbers in your
.cpp
file.Start debugging by pressing
F5
or using the Run view.
Conclusion
Now you have a fully functional C++ development environment in VS Code. You can write, build, and run your C++ programs directly within the editor.