Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt

Use this file to discover all available pages before exploring further.

link.exe (LINK) is the Microsoft linker. It takes COFF object files (.obj), import libraries (.lib), and resource files (.res) as input and produces executables (.exe), dynamic-link libraries (.dll), and static libraries managed by lib.exe. LINK is automatically invoked when you compile and link in a single cl.exe command, or you can call it directly for finer control. Linker options are not case-sensitive — /out and /OUT are equivalent. Linker options can be specified on the command line, in a response file (@file.rsp), or via #pragma comment(linker, "/OPTION") in source code.
link.exe must be run from a Visual Studio Developer Command Prompt or after calling vcvarsall.bat. See Building from the Command Line for environment setup.

Output File Options

Control where and how the linker writes its output.
OptionDescription
/OUT:<file>Specifies the name of the output file (.exe or .dll)
/PDB:<file>Creates a Program Database file with debug symbols
/PDBSTRIPPED:<file>Creates a PDB with private symbols stripped (for distribution)
/MAP[:<file>]Generates a map file listing all public symbols, section layout, and entry points
/MAPINFO:EXPORTSInclude exported functions in the map file
/ILK:<file>Override the default name of the incremental link database
# Specify output name and PDB location
link /OUT:bin\myapp.exe /PDB:symbols\myapp.pdb main.obj engine.obj

# Generate a map file for symbol analysis
link /OUT:bin\myapp.exe /MAP:bin\myapp.map /MAPINFO:EXPORTS main.obj

# Create a stripped PDB for distribution (no private symbols)
link /OUT:bin\myapp.exe ^
     /PDB:internal\myapp.pdb ^
     /PDBSTRIPPED:dist\myapp_public.pdb ^
     main.obj engine.obj

Library Options

Control which libraries are searched and how they are found.
OptionDescription
/LIBPATH:<dir>Add a directory to the library search path (checked before LIB environment variable)
/DEFAULTLIB:<lib>Add a library to the default set linked at the end
/NODEFAULTLIB[:<lib>]Ignore all (or a named) default library; useful for custom CRT or OS-level code
/WHOLEARCHIVE[:<lib>]Include all object files from the specified static library (overrides dead-stripping)
# Add a custom library directory and link a specific library
link /LIBPATH:C:\libs\mylib\lib\x64 ^
     /OUT:myapp.exe ^
     myapp.obj mylib.lib kernel32.lib

# Ignore all default libraries and provide your own
link /NODEFAULTLIB ^
     /OUT:myruntimelib.exe ^
     main.obj myruntime.lib

# Force all object files from a static library to be included
link /WHOLEARCHIVE:plugins.lib /OUT:host.exe host.obj kernel32.lib

Subsystem Options

The /SUBSYSTEM option tells the Windows loader which entry point to expect and how to launch the process.
OptionEntry PointUse Case
/SUBSYSTEM:CONSOLEmain() / wmain()Command-line applications; allocates a console window
/SUBSYSTEM:WINDOWSWinMain() / wWinMain()GUI applications; no console window
/SUBSYSTEM:NATIVENtProcessStartup()Device drivers and NT native applications
/SUBSYSTEM:EFI_APPLICATIONUEFI applications
# Console application (default for most C++ programs)
link /SUBSYSTEM:CONSOLE /OUT:myapp.exe main.obj kernel32.lib

# GUI Windows application
link /SUBSYSTEM:WINDOWS /OUT:mygui.exe winmain.obj user32.lib gdi32.lib kernel32.lib

# Specify minimum required Windows version (Windows 10 = 10.0)
link /SUBSYSTEM:CONSOLE,10.0 /OUT:myapp.exe main.obj kernel32.lib

Debug Options

OptionDescription
/DEBUGGenerate debug info (full PDB); implies /OPT:NOREF /OPT:NOICF
/DEBUG:FASTLINKCreate a PDB that references object file debug info rather than copying it — much faster for large builds, unsuitable for distribution
/DEBUG:FULLCopy all debug info into the PDB (default when /DEBUG is alone)
/DEBUGTYPE:<type>Controls what data goes into the debug info (e.g., CV, PDATA, FIXUP)
# Standard debug build with full PDB
link /DEBUG /OUT:myapp.exe main.obj engine.obj

# Fast linking during development (PDB references obj files, not self-contained)
link /DEBUG:FASTLINK /OUT:myapp.exe main.obj engine.obj

# Release build with distributable PDB
link /DEBUG:FULL /OPT:REF /OPT:ICF /OUT:myapp.exe main.obj engine.obj

Optimization Options

OptionDescription
/OPT:REFEliminate unreferenced functions and data (COMDATs) — enabled by default in release builds
/OPT:NOREFKeep all functions and data even if not referenced
/OPT:ICF[=n]Identical COMDAT Folding — merge duplicate functions/data to reduce size. n is iteration count
/OPT:NOICFDisable COMDAT folding
/LTCGLink-Time Code Generation — enables whole-program optimization across all modules
/LTCG:INCREMENTALIncremental LTCG — reoptimize only changed files for faster incremental links
/OPT:LBRLong Branch Reduction for ARM binaries
# Typical release build optimizations
link /OPT:REF /OPT:ICF /OUT:myapp.exe main.obj engine.obj kernel32.lib

# Full whole-program optimization with LTCG
# (requires all .obj files compiled with /GL)
link /LTCG /OPT:REF /OPT:ICF /OUT:myapp.exe main.obj engine.obj kernel32.lib

# Incremental LTCG for faster dev-release builds
link /LTCG:INCREMENTAL /OPT:REF /OPT:ICF /OUT:myapp.exe main.obj engine.obj
/OPT:REF and /OPT:ICF are automatically disabled when /DEBUG is specified, since folding functions would break the debugger. For debug builds that still need a small binary, explicitly pass /OPT:REF /OPT:NOICF.

DLL Options

OptionDescription
/DLLBuild a dynamic-link library instead of an executable
/EXPORT:<symbol>Export a function or variable by name
/EXPORT:<symbol>,DATAExport a data symbol
/IMPLIB:<file>Override the default name for the generated import library
/DEF:<file>Use a Module Definition File (.def) for exports
/NOENTRYCreate a resource-only DLL with no entry point
/DYNAMICBASEEnable ASLR (Address Space Layout Randomization) — enabled by default
/NXCOMPATMark as Data Execution Prevention (DEP) compatible — enabled by default
# Create a DLL with an import library
link /DLL ^
     /OUT:bin\mylib.dll ^
     /IMPLIB:bin\mylib.lib ^
     /PDB:bin\mylib.pdb ^
     mylib.obj ^
     kernel32.lib msvcrt.lib

# Export specific functions
link /DLL /OUT:plugin.dll ^
     /EXPORT:PluginInit ^
     /EXPORT:PluginShutdown ^
     /EXPORT:PluginVersion,DATA ^
     plugin.obj kernel32.lib

# Use a .def file for exports
link /DLL /OUT:mylib.dll /DEF:mylib.def mylib.obj kernel32.lib
Example .def file:
LIBRARY mylib
EXPORTS
    MyFunction          @1
    MyOtherFunction     @2
    MyDataVariable      @3 DATA

Profile-Guided Optimization (PGO) Linker Options

PGO is a three-step process that requires specific linker options at each stage. See Optimizations for the full workflow.
OptionPGO PhaseDescription
/GENPROFILEStep 1 (Instrument)Generate a profiling-instrumented build with a .pgd file
/FASTGENPROFILEStep 1 (Instrument)Like /GENPROFILE but uses sampling (faster instrumentation)
/USEPROFILEStep 3 (Optimize)Use collected profile data (.pgd/.pgc) to optimize
/PGD:<file>All stepsSpecify the .pgd database file
/LTCGSteps 1 and 3Required for PGO
# Step 1: Build instrumented binary
link /LTCG /GENPROFILE /PGD:myapp.pgd /OUT:myapp_instr.exe main.obj engine.obj

# Step 2: Run the instrumented binary with representative workloads
myapp_instr.exe --benchmark

# Step 3: Link optimized binary using collected profile data
link /LTCG /USEPROFILE /PGD:myapp.pgd /OUT:myapp_opt.exe main.obj engine.obj

Security and Safety Options

OptionDescription
/DYNAMICBASEEnable ASLR (on by default)
/NXCOMPATEnable DEP/NX (on by default)
/HIGHENTROPYVA64-bit ASLR with high-entropy virtual addresses (on by default for x64)
/GUARD:CFEnable Control Flow Guard
/SAFESEHRegister exception handlers in a safe exception handler table (x86 only)
/INTEGRITYCHECKRequire a digital signature check at load time
# Explicitly enable all modern security features
link /DYNAMICBASE /NXCOMPAT /HIGHENTROPYVA /GUARD:CF ^
     /OUT:myapp.exe main.obj kernel32.lib

Practical Examples

link ^
  /OUT:bin\myapp.exe ^
  /PDB:symbols\myapp.pdb ^
  /DEBUG ^
  /SUBSYSTEM:CONSOLE ^
  /OPT:REF ^
  /OPT:ICF ^
  /LTCG ^
  /DYNAMICBASE ^
  /NXCOMPAT ^
  obj\release\*.obj ^
  kernel32.lib ^
  msvcrt.lib ^
  user32.lib

Linker Options Quick Reference

CategoryOptionDescription
Output/OUT:<file>Output file name
Output/PDB:<file>PDB file name
Output/MAPGenerate map file
Output/IMPLIB:<file>Import library name
Libraries/LIBPATH:<dir>Extra library search directory
Libraries/DEFAULTLIB:<lib>Add to default libraries
Libraries/NODEFAULTLIBIgnore all default libraries
Subsystem/SUBSYSTEM:CONSOLEConsole application
Subsystem/SUBSYSTEM:WINDOWSWindows GUI application
Debug/DEBUGFull debug PDB
Debug/DEBUG:FASTLINKFast local PDB
Optimization/OPT:REFRemove unreferenced code
Optimization/OPT:ICFFold identical COMDATs
Optimization/LTCGLink-time code generation
DLL/DLLBuild a DLL
DLL/EXPORT:<sym>Export a symbol
DLL/DEF:<file>Module definition file
Security/DYNAMICBASEEnable ASLR
Security/GUARD:CFControl Flow Guard
PGO/GENPROFILEInstrument for PGO
PGO/USEPROFILEOptimize using profile data

Build docs developers (and LLMs) love