Master MSVC: Install, Configure, and Compile C/C++ on Windows
This guide introduces Microsoft Visual C++ (MSVC), covering its key features, installation via Visual Studio or Build Tools, command‑line and VS Code integration, step‑by‑step compilation commands, and advanced options like parallel builds and PGO, helping developers efficiently build C/C++ projects on Windows.
Overview
Microsoft Visual C++ (MSVC) is the C and C++ compiler bundled with Visual Studio and also available through Visual Studio Build Tools. It implements the full C++11‑C++20 language standards, offers aggressive optimizations, integrates a debugger, and ships with libraries such as the STL, MFC, and ATL.
Installation and configuration
Visual Studio
Download the latest installer from the official Visual Studio website.
Select the Desktop development with C++ workload to install the compiler, linker, and Windows SDK.
Visual Studio Build Tools (for command‑line use only)
Download the Build Tools installer from Microsoft’s site.
Choose the Desktop development with C++ workload.
Or install via winget:
winget install Microsoft.VisualStudio.2022.BuildTools --force --override "--wait --passive --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.Windows11SDK.22000"Compiling C/C++ programs with MSVC
Open the Developer Command Prompt
Search for x64 Native Tools Command Prompt for VS 2022 (or the version you installed) and launch it.
Compile a source file
Navigate to the directory containing the source code.
Run the compiler: cl /EHsc your_program.cpp This produces your_program.exe .
Using MSVC in Visual Studio Code
Install VS Code and the official Microsoft C/C++ extension.
Create a build task (add the following to .vscode/tasks.json):
{
"version": "2.0.0",
"tasks": [
{
"label": "cl.exe build",
"type": "shell",
"command": "cl",
"args": [
"/EHsc",
"${file}",
"/Fe:${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"group": { "kind": "build", "isDefault": true },
"problemMatcher": ["$msCompile"],
"detail": "Generated task by Visual Studio Code"
}
]
}Write and build a program Example hello.cpp :
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}Save the file and press Ctrl+Shift+B to invoke the task and produce hello.exe . Launch VS Code from the MSVC developer prompt (e.g., run code . ) so that the environment variables are inherited.
Advanced MSVC features
Parallel compilation : Add the /MP flag to enable multithreaded builds, e.g., cl /EHsc /MP your_program.cpp.
Microsoft‑specific extensions : Keywords such as __declspec allow custom storage‑class specifications and DLL export/import annotations.
Profile‑Guided Optimization (PGO) : Build the program, run it to collect performance data, then re‑compile with the /GL and /LTCG:PGINSTRUMENT (instrument) and /LTCG:PGOPTIMIZE (optimize) switches to produce a faster binary.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
