This is a template project for compiling and debugging C/C++ (.c or .cpp) files using the Microsoft C++ (MSVC) compiler directly within VisualStudio Code.
The main goal is to enable compiling and debugging with F5 without needing to launch VS Code from the "Developer Command Prompt for VS".
Before using this template, you must have the following software installed:
- Visual Studio Code: The code editor;
- Microsoft C/C++ Extension: The official extension (
ms-vscode.cpptools) for VS Code; - MSVC C++ build tools: The Microsoft C++ compiler. You can install the
Desktop development with C++workload without a full Visual Studio IDE installation. From the Visual Studio Downloads page, scroll down until you seeTools for Visual Studiounder theAll Downloadssection and select the download forBuild Tools for Visual Studio 2022. This will launch the Visual Studio Installer, which will bring up a dialog showing the available Visual Studio Build Tools workloads. Check theDesktop development with C++workload and select Install.
This template expects the following directory structure:
my_project/
│
├── .vscode/
│ ├── tasks.json # Build task (compiles project)
│ ├── launch.json # Debugger config (launches executable)
│ └── c_cpp_properties.json # IntelliSense config
│
├── bin/ # Folder for the final compiled executable
│ └── my_project.exe
│
├── build/ # Folder for intermediate object files
│ └── main.obj # Where intermediate object files (.obj) are located
│ └── utils.obj
│
├── include/ # Folder for header files (.h, .hpp)
│ └── utils.h
│
├── src/ # Folder for source files (.cpp)
│ ├── main.cpp
│ └── utils.cpp
│
├── lib/ # (Optional) Third-party libraries
│
├── .gitignore
└── README.md
After cloning this repository, you must update two paths in the .vscode configuration files to match your local installation of Visual Studio / Build Tools.
MSVC is not added to the global Windows PATH and relies on scripts to set up the environment. Our configuration files need to know where these scripts are located.
You will need to locate two files from your Visual Studio installation:
VsDevCmd.bat: The main script that configures the developer environment;- Common location:
C:\Program Files\Microsoft Visual Studio\[YEAR]\[EDITION]\Common7\Tools\VsDevCmd.bat
- Common location:
cl.exe: The compiler executable.- Common location:
C:/Program Files (x86)/Microsoft Visual Studio/[YEAR]/BuildTools/VC/Tools/MSVC/[VERSION]/bin/Hostx64/x64/cl.exe
- Common location:
Tip: An easy way to find the root folder is to search for "Developer Command Prompt" in your Start Menu, right-click it, and select "Open file location." The shortcut will point to the
VsDevCmd.batlocation.
Open the .vscode folder and edit the following files:
1. .vscode/tasks.json
This file is responsible for running the build task. It needs to know where VsDevCmd.bat is.
- Change line 11: Replace the path
"I:/Visual Studio Compiler/Common7/Tools/VsDevCmd.bat"with the path you found in Step 1.
{
"version": "2.0.0",
"windows": {
"options": {
"shell": {
"executable": "cmd.exe",
"args": [
"/C",
// V-- CHANGE THIS LINE --V
"\"I:/Visual Studio Compiler/Common7/Tools/VsDevCmd.bat\"",
"&&"
]
}
}
},
"tasks": [
{
"type": "shell",
"label": "cl.exe build project",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/I", "${workspaceFolder}/include", // Include directory
"/Fe:", "${workspaceFolder}/bin/my_project.exe", // Output executable
"${workspaceFolder}/src/*.cpp" // Source files
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
2. .vscode/c_cpp_properties.json
This file tells IntelliSense (autocomplete and error checking) where to find the compiler.
- Change line 13: Replace the path
"I:\\Visual Studio Compiler\\...\\cl.exe"with your cl.exe path from Step 1; - Note: In JSON files, backslashes (
\) must be escaped (doubled up as\\).
{
"configurations": [
{
"name": "Win32",
// ...
"intelliSenseMode": "windows-msvc-x64",
// V-- CHANGE THIS LINE (use double backslashes \\) --V
"compilerPath": "\"I:\\Visual Studio Compiler\\VC\\Tools\\MSVC\\14.44.35207\\bin\\Hostx64\\x64\\cl.exe\""
}
],
"version": 4
}
After you have configured the paths correctly and structured your project with src/ and include/ folders:
- Open any
.cor.cppfile in VS Code; - Press
F5(or go to "Run" > "Start Debugging"); - VS Code will:
- Execute the
preLaunchTaskdefined inlaunch.json; - This task (from
tasks.json) will first load the environment usingVsDevCmd.bat; - It will then compile all
.cppfiles in thesrc/folder, link them, and output a single executable to thebin/folder (e.g.,bin/my_project.exe); - After a successful compilation, the VS Code debugger will launch and attach to your program.
- Execute the
This configuration is based on the official Visual Studio Code documentation. For more details, see: https://code.visualstudio.com/docs/cpp/config-msvc