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"Set up C/C++: MSVC build tools, MinGW-w64 and GCC/Clang, CMake projects, vcpkg dependency management, debuggers and common error fixes.
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.UCRTchoco 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-essentialInstall CMake and check the version; cross-platform projects use it to generate build files
winget install Kitware.CMake
# macOS
brew install cmake
cmake --versionInstall the Ninja backend; CMake with Ninja compiles noticeably faster than make
winget install Git.Git
winget install Git.Git.NinjaBootstrap vcpkg on Windows to install third-party C/C++ libraries
git clone https://github.com/microsoft/vcpkg
cd vcpkg
.\bootstrap-vcpkg.batInstall packages per triplet; use x64-windows on Windows and the matching triplet when cross-compiling
vcpkg install fmt:x64-windows
vcpkg install openssl:x64-linuxLet Visual Studio and MSBuild pick up libraries installed by vcpkg automatically
vcpkg integrate installReplace 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:7890Check 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 --versionSet 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"' >> ~/.zshrcCheck whether the MinGW bin directory is really on PATH on Windows
echo %PATH% | findstr /i mingwLoad 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 clCreate a project directory and a source file with a standard int main that prints Hello
mkdir hello && cd hello
code main.cppCompile and run with GCC and common warnings enabled; Windows produces an .exe
g++ -std=c++20 -Wall -Wextra main.cpp -o hello
.\helloWrite 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 buildPoint CMake at vcpkg-provided dependencies; shown in PowerShell syntax
cmake -B build -DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_ROOT/scripts/buildsystems/vcpkg.cmakeCommand-line debuggers for tracking down crashes with break, run and bt
gdb ./hello
# macOS
lldb ./helloInstall the C/C++ extension and CMake Tools for IntelliSense, builds and debugging
code --install-extension ms-vscode.cpptools
code --install-extension ms-vscode.cmake-toolsInstall CLion on Windows; use brew --cask clion on macOS
winget install JetBrains.CLionInstall CodeLLDB; LLDB is smoother than gdb for debugging on macOS/Linux
code --install-extension vadimcn.vscode-lldbWhen g++ or cl is missing, confirm the toolchain is installed and its bin directory is on PATH
# Windows
where g++
where clFilter compiler output down to error lines to find the first failure fast
g++ main.cpp -o hello 2>&1 | findstr errorCheck for missing shared library dependencies on Linux
ldd ./helloEnable AddressSanitizer to catch out-of-bounds access and leaks
g++ -g -fsanitize=address main.cpp -o helloSwitch the Windows console code page to UTF-8 when Chinese text is garbled
chcp 65001Clean before rebuilding when incremental builds produce stale artifacts
cmake --build build --clean-firstThis website uses cookies to ensure you get the best experience on our website. Privacy Policy.