MSVC cl.exe and clang-cl
Contract
| Field | Bound contract |
|---|---|
| Trigger | The user compiles on Windows with cl.exe or clang-cl, needs the MSVC spelling of a GCC flag, must pick a runtime library, wants PDB files, or has an LNK error. |
| Authority | Read-only. The skill emits flags, commands, and CMake settings to chat; the user runs them. Rollback is not needed. No remote mutation. |
| Side effect | Chat output. |
| Done | The flag set, runtime choice, and debug-info settings are reported, every flag is confirmed against the Microsoft compiler-options reference or clang-cl /?, and every LNK code in the request has a cause and fix. |
Inputs
- Compiler: required.
cl.exefrom a Visual Studio install, orclang-clfrom an LLVM release or the Visual Studio LLVM component. Current upstream Clang is 23.1.0. - Target architecture: required. x64 or x86; selects the
vcvarsscript. - Build system: required. Command line, MSBuild, or CMake.
- The GCC or Clang flags being translated: required for translation requests.
- The exact
LNKmessage: required for link diagnosis.
Procedure
Set up the environment. Open a Developer Command Prompt, or run
"C:\Program Files\Microsoft Visual Studio\2022\<edition>\VC\Auxiliary\Build\vcvars64.bat"for x64 native andvcvarsamd64_x86.batfor an x86 target from an x64 host. Confirm withcl /?. With CMake, pass-G "Visual Studio 17 2022". Done when:clruns from the shell.Translate flags from the table. Done when: each requested flag has its MSVC spelling.
GCC or Clang cl.exe -O0/Od-O1/O1(size)-O2/O2(speed)-O3/Ox-Os/Os-g/Zi(external PDB) or/Z7(in the object)-Wall/W4;/Wallis far noisier than GCC's-Werror/WX-Wno-xxxx/wd<code>-std=c++20/std:c++20; add/permissive-for conformance-DFOO=1/DFOO=1-I dir/I dir-c/c-o out/Fe:out.exeor/Fo:out.obj-shared/LD-fPICNot needed on Windows -flto/GLat compile and/LTCGat linkChoose the runtime library, the most common source of link errors:
/MD(dynamicMSVCRT, the default for release),/MDd(dynamic debug),/MT(staticLIBCMT),/MTd(static debug). Every object and library in one link must use the same choice. Done when: one runtime is chosen for the whole link.Use clang-cl where wanted: it accepts
cl.exeflags, soclang-cl /O2 /std:c++20 /MD src.cpp /Fe:prog.exeworks, and/clang:<arg>passes a Clang-native flag through, for example/clang:-Rpass=inline. In CMake:-DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl. Microsoft documents Clang 8 as the minimum for its MSBuild integration; use the current LLVM release. Done when: the clang-cl command or CMake setting is stated.Produce debug information.
cl /Zi /Fd:prog.pdb /O2 src.cpp /link /DEBUGwrites an external PDB;cl /Z7 /O2 src.cpp /link /DEBUG /PDB:prog.pdbembeds debug data in each object and lets the linker build the PDB. The linker's/DEBUG:FASTLINKlinks faster but needs the object PDBs present when debugging. Keep the PDB beside the executable or on the debugger's symbol path. Done when: the PDB flags match the workflow.Diagnose
LNKerrors from the table. Done when: each code has a cause and fix.Error Cause Fix LNK2019: unresolved externalA .libis missing from the linkAdd it in project settings or /link foo.libLNK2038: mismatch detected for 'RuntimeLibrary'/MTand/MDobjects mixedOne runtime for every object and library LNK1104: cannot open file 'foo.lib'Library not on the search path /LIBPATH:<dir>or theLIBvariableLNK2005: already definedA symbol defined in two objects Remove the duplicate; __declspec(selectany)for intentionally repeated dataLNK4098: defaultlib 'LIBCMT' conflictsRuntime mismatch between libraries Unify runtimes, or /NODEFAULTLIB:LIBCMTas documented for that warningAnswer inspection questions with the exact flag:
/Pwrites preprocessed output to.i,/FAwrites an assembly listing and/FAsinterleaves source,/showIncludesprints the include tree,/analyzeruns static analysis,/Zadisables Microsoft extensions. Linker flags after/link:/SUBSYSTEM:CONSOLEor/SUBSYSTEM:WINDOWS,/DLL,/ENTRY:fn,/INCREMENTAL:NO,/LIBPATH:,/NODEFAULTLIB:. Done when: each question maps to one flag.Confirm every flag:
clang-cl /?lists the ones it accepts on any host; forcl.exe-only flags cite the Microsoft compiler-options reference. Done when: no unconfirmed flag remains.
Failure and recovery
| Failure class | Behavior |
|---|---|
cl not found |
The vcvars script has not run in this shell; give the script path for the target. |
LNK code not in the table |
Give the Microsoft error page for that code rather than a guess. |
Flag absent from clang-cl /? and from the reference |
Drop it and say so. |
| Cross-compiling from Linux | MinGW triplets: use cross-gcc; Zig's x86_64-windows-gnu: use zig-cross. |
| Clang-native diagnostics wanted | Pass through /clang: and consult clang for the flag set. |
Output
A chat report with the environment command, the translated flag set, the runtime choice with the rule that every object must match, the PDB flags, and the LNK entries that apply, each flag traced to clang-cl /? or the Microsoft reference.