Msvc Cl

mohitmishra786/low-level-dev-skills/skills/compilers/msvc-cl

作者 mohitmishra786bdc58472fa9f無授權條款253 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫3 個月前更新

MSVC cl.exe and clang-cl skill for Windows C/C++ projects. Use when configuring Visual Studio builds, MSBuild, or clang-cl as a drop-in MSVC replacement. Covers translating GCC/Clang flags to MSVC equivalents, runtime library selection, Windows SDK setup, and diagnosing MSVC-specific errors. Activates on queries about cl.exe, clang-cl, /O flags, /MT vs /MD, PDB files, Windows ABI, or MSVC project settings.

AI 產生的概覽

指導代理使用 MSVC cl.exe 和 clang-cl 在 Windows 上編譯 C/C++,涵蓋旗標、執行階段程式庫和連結錯誤。

功能
此技能為在 Windows 上使用 MSVC cl.exe 和 clang-cl 編譯 C 與 C++ 提供參考指引。內容涵蓋使用 vcvars 指令碼設定環境、將 GCC/Clang 旗標轉換為 MSVC 等效旗標、在 /MT 與 /MD 執行階段程式庫之間選擇、產生 PDB 偵錯資訊,以及診斷常見的 LNK 錯誤。它也包含一個提供更多旗標細節的參考檔案。
適用情境
適用於在 Windows 上設定 Visual Studio、MSBuild 或 CMake 建置,或以 clang-cl 取代 cl.exe 的情況。也適合解決 MSVC 特有的編譯或連結錯誤,例如執行階段程式庫不相符。
執行需求
不包含指令碼,僅為指示與參考文件。需要可存取已安裝 Visual Studio 或 LLVM/clang-cl 的 Windows 環境,代理可能需要執行 vcvars 指令碼或編譯器命令。

MSVC cl.exe and clang-cl

Purpose

Guide agents through Windows C/C++ compilation: MSVC cl.exe, clang-cl as MSVC-compatible driver, MSBuild project settings, and runtime library choices.

Triggers

  • "How do I compile with MSVC from the command line?"
  • "What is the MSVC equivalent of -O2?"
  • "/MT vs /MD — which do I use?"
  • "How do I use clang-cl instead of cl.exe?"
  • "I'm getting LNK errors on Windows"
  • "How do I generate PDB files?"

Workflow

1. Set up the environment

MSVC requires the Visual Studio environment variables. Use the Developer Command Prompt or set up manually:

cmd
REM x64 native"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
REM x86 cross (host x64, target x86)"...\vcvarsamd64_x86.bat"
REM Check compiler versioncl /?

In CMake, use the "Visual Studio 17 2022" generator or pass -DCMAKE_GENERATOR="Visual Studio 17 2022".

2. Flag translation: GCC → MSVC

GCC/ClangMSVC cl.exeNotes
-O0/OdDebug, no optimisation
-O1/O1Minimise size
-O2/O2Maximise speed
-O3/OxFull optimisation
-Os/OsFavour size
-g/ZiPDB debug info
-g (embedded)/Z7Debug info in object file
-Wall/W3 or /W4Warning level
-Werror/WXWarnings as errors
-std=c++17/std:c++17Standard selection
-DFOO=1/DFOO=1Preprocessor define
-I path/I pathInclude path
-c/cCompile only (no link)
-o out/Fe:out.exe or /Fo:out.objOutput file
-shared/LDBuild DLL
-fPIC(implicit on Windows)Not needed on Windows
-flto/GL (compile) + /LTCG (link)Whole program optimisation

3. Runtime library selection

This is the most common source of LNK errors on Windows.

FlagRuntimeUse case
/MDMSVCRT.dll (dynamic)Release, DLL linkage (recommended default)
/MDdMSVCRTd.dll (debug dynamic)Debug builds
/MTStatic CRTStandalone exe; avoid mixing with DLLs
/MTdStatic CRT debugDebug + static

Rule: All objects and libraries in a link must use the same runtime. Mixing /MT and /MD causes LNK2038: mismatch detected.

4. clang-cl

clang-cl is Clang's MSVC-compatible driver. It accepts cl.exe-style flags and can replace cl.exe in most MSBuild/CMake projects.

cmd
REM Install: part of LLVM Windows release or VS "LLVM" component
REM Basic usage (MSVC-style flags)clang-cl /O2 /std:c++17 /MD src.cpp /Fe:prog.exe
REM Pass Clang-native flags with /clang:clang-cl /O2 /clang:-Rpass=inline src.cpp /Fe:prog.exe
REM Use in CMakecmake -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl ..

Minimum Clang version for MSVC STL compatibility: Clang 8+. For C++20 features with MSVC STL: Clang 14+.

Source: https://learn.microsoft.com/en-us/cpp/build/clang-support-msbuild

5. PDB files and debugging

cmd
REM Compile with /Zi (external PDB) and /Fd (PDB name)cl /Zi /Fd:prog.pdb /O2 src.cpp /link /DEBUG
REM /Z7: debug info embedded in .obj (no separate PDB for objects)cl /Z7 /O2 src.cpp /link /DEBUG /PDB:prog.pdb

For WinDbg or VS debugger, ensure the PDB is alongside the executable or set the symbol path.

6. Common LNK errors

ErrorCauseFix
LNK2019: unresolved externalMissing .libAdd library in project settings or /link foo.lib
LNK2038: mismatch detected for 'RuntimeLibrary'Mixed /MT and /MDUnify all to /MD or /MT
LNK1104: cannot open file 'foo.lib'Library not in search pathAdd path via /LIBPATH: or LIB env var
LNK2005: already definedMultiple definitionsUse __declspec(selectany) or check for duplicate definitions
LNK4098: defaultlib 'LIBCMT' conflictsRuntime mismatchExplicitly pass /NODEFAULTLIB:LIBCMT or unify runtimes

7. Useful cl.exe flags

cmd
REM Preprocessed outputcl /P src.cpp       # produces src.i
REM Assembly outputcl /FA /O2 src.cpp  # produces src.asm (MASM syntax)cl /FAs             # interleave source + asm
REM Show include filescl /showIncludes src.cpp
REM Compiler versioncl /?  2>&1 | findstr /i "version"

For flag details, see references/flags.md [blocked].

Related skills

  • Use skills/compilers/clang for clang-cl's Clang-native diagnostics
  • Use skills/build-systems/cmake for CMake toolchain configuration on Windows
  • Use skills/debuggers/lldb or WinDbg for debugging MSVC-built binaries

來源與署名

來源:mohitmishra786/low-level-dev-skills位於skills/compilers/msvc-cl提交bdc5847

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架