C/C++ Setup Guide - CodeSwordsman

Set up C/C++: MSVC build tools, MinGW-w64 and GCC/Clang, CMake projects, vcpkg dependency management, debuggers and common error fixes.

MSVC 2022 / GCC 13 / Clang 17Updated 2026-10-2929 commands

Install & Version Management

Install the MSVC compiler and Windows SDK on Windows without the full IDE

winget install Microsoft.VisualStudio.2022.BuildTools --override "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"

Install MinGW-w64 (GCC) on Windows for lightweight CLI builds and portable code

winget install BrechtSanders.WinLibs.POSIX.UCRT

choco install mingw works too; make sure its bin directory ends up on PATH

Install the Xcode command line tools (Clang) on macOS and build-essential (GCC + make) on Ubuntu

# macOS
xcode-select --install
# Ubuntu / Debian
sudo apt update && sudo apt install -y build-essential

Install CMake and check the version; cross-platform projects use it to generate build files

winget install Kitware.CMake
# macOS
brew install cmake
cmake --version

Install the Ninja backend; CMake with Ninja compiles noticeably faster than make

winget install Git.Git
winget install Git.Git.Ninja

Mirrors & Dependencies

Bootstrap vcpkg on Windows to install third-party C/C++ libraries

git clone https://github.com/microsoft/vcpkg
cd vcpkg
.\bootstrap-vcpkg.bat

Install packages per triplet; use x64-windows on Windows and the matching triplet when cross-compiling

vcpkg install fmt:x64-windows
vcpkg install openssl:x64-linux

Let Visual Studio and MSBuild pick up libraries installed by vcpkg automatically

vcpkg integrate install
Risky

Replace GitHub with a mirror when clones are slow; only works for public repositories

git config --global url."https://gitee.com/mirrors/".insteadOf "https://github.com/"

This changes every git clone globally; unset it once you are done

Route git and package managers through a proxy; use set on Windows

export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890

Environment Variables

Check which toolchain the compiler on PATH belongs to; critical when several are installed

# Windows
where gcc
gcc --version
# macOS / Linux
which -a gcc clang
gcc --version

Set VCPKG_ROOT so CMake and IDE extensions can locate vcpkg

# Windows
setx VCPKG_ROOT "C:\dev\vcpkg"
# macOS / Linux
echo 'export VCPKG_ROOT="$HOME/vcpkg"' >> ~/.zshrc

Check whether the MinGW bin directory is really on PATH on Windows

echo %PATH% | findstr /i mingw

Load the MSVC environment into a plain terminal so cl.exe becomes available

# Windows(开发者命令提示符)
call "C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
where cl

Create & Verify

Create a project directory and a source file with a standard int main that prints Hello

mkdir hello && cd hello
code main.cpp

Compile and run with GCC and common warnings enabled; Windows produces an .exe

g++ -std=c++20 -Wall -Wextra main.cpp -o hello
.\hello

Write a CMakeLists.txt so CMake can generate build files for every platform

cmake_minimum_required(VERSION 3.20)
project(hello CXX)
set(CMAKE_CXX_STANDARD 20)
add_executable(hello main.cpp)

Configure a build directory and compile; the Debug type enables breakpoint debugging

cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build build

Point CMake at vcpkg-provided dependencies; shown in PowerShell syntax

cmake -B build -DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake

Command-line debuggers for tracking down crashes with break, run and bt

gdb ./hello
# macOS
lldb ./hello

IDE & Plugins

Install the C/C++ extension and CMake Tools for IntelliSense, builds and debugging

code --install-extension ms-vscode.cpptools
code --install-extension ms-vscode.cmake-tools

Install CLion on Windows; use brew --cask clion on macOS

winget install JetBrains.CLion

Install CodeLLDB; LLDB is smoother than gdb for debugging on macOS/Linux

code --install-extension vadimcn.vscode-lldb

Troubleshooting

When g++ or cl is missing, confirm the toolchain is installed and its bin directory is on PATH

# Windows
where g++
where cl

Filter compiler output down to error lines to find the first failure fast

g++ main.cpp -o hello 2>&1 | findstr error

Check for missing shared library dependencies on Linux

ldd ./hello

Enable AddressSanitizer to catch out-of-bounds access and leaks

g++ -g -fsanitize=address main.cpp -o hello

Switch the Windows console code page to UTF-8 when Chinese text is garbled

chcp 65001

Clean before rebuilding when incremental builds produce stale artifacts

cmake --build build --clean-first

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.