Microsoft C++ Standard Library (STL)

repository·main·Indexed 11 days ago

https://github.com/microsoft/stl

Official implementation of Microsoft's C++ Standard Library, integrated into the MSVC toolset and Visual Studio IDE. This documentation provides guidance for developers on building the STL from source, reporting implementation bugs, running test suites via stl-lit.py, and configuring benchmarks using the Google Benchmark library.

Tokens
4K
Snippets
10
Records
16
Agent score
45%

What's inside Microsoft STL

  1. Understand the behavior and limitations of the STL import library

    main

    The STL's import library (.lib) is unique because it does more than just define symbols imported from a DLL; it also defines certain functions and variables directly within the library. This allows for extending the STL (e.g., adding <filesystem> support) without changing the DLL export surface and improves throughput by compiling constant data (like <charconv> lookup tables) separately.

    Critical Limitations

    When working with or building against this library, be aware of these constraints:

    • No Shared Global State: Because the import library embeds part of the implementation into every user binary that links to it, variables in the import library cannot represent shared global state. If your application requires shared global state across DLL boundaries, a satellite DLL must be used instead.
    • Bypassing /MD and /MDd: This technique embeds part of the STL implementation into the user's binary, which partially defeats the purpose of using the /MD (Release) or /MDd (Debug) compiler options.
    • Iterator Debug Level Constraints: Because there are only two flavors of the import library (Debug and Release), the library cannot use anything that depends on _ITERATOR_DEBUG_LEVEL.
    • Implementation Restrictions: To avoid the issues above, the import library is restricted to core headers only. For example, basic_string must not be used within the import library.
  2. Build ARM64 natively

    main

    By default, ARM64 and ARM64EC presets assume cross-compilation and enable TESTS_BUILD_ONLY (building tests without running them). If you are on an ARM64 machine and want to build and run tests natively, you must disable this option using -DTESTS_BUILD_ONLY=OFF.

    pushd "%ProgramFiles%\Microsoft Visual Studio\18\Insiders\VC\Auxiliary\Build"
    vcvarsall.bat arm64 -vcvars_ver=preview
    popd
    cmake --preset ARM64 -DTESTS_BUILD_ONLY=OFF
    cmake --build --preset ARM64
  3. Modify and test the Debugger Visualizer

    main

    The STL uses a .natvis file to control how components are visualized in the Visual Studio debugger.

    • To modify visualization: Edit stl\debugger\STL.natvis.
    • To test changes: Add the STL.natvis file to any Visual Studio C++ project (Right-click project > Add > Existing Item) to see the updated visualizations during a debugging session.
  4. Configure benchmark compilation options

    main

    You can influence how benchmarks are compiled using environment variables or CMake flags:

    • Compiler Flags: Use the CXXFLAGS environment variable to pass additional options (e.g., /arch:AVX2) to the benchmark compilation. This must be set after building the STL but before configuring/building the benchmarks.
    • Compiler Choice: To use Clang instead of MSVC, pass -DCMAKE_CXX_COMPILER=clang-cl during the benchmark configuration step.
    • Execution Priority: To run a benchmark with high priority or on specific CPU cores, use the Windows start command with /high and /affinity.
    # Use specific architecture flags
    set CXXFLAGS=/arch:AVX2
    cmake -B out\bench -S benchmarks -G Ninja -DSTL_BINARY_DIR=out\x64
    cmake --build out\bench
    
    # Use Clang
    cmake -B out\bench -S benchmarks -G Ninja -DSTL_BINARY_DIR=out\x64 -DCMAKE_CXX_COMPILER=clang-cl
    
    # Run with high priority and affinity
    start /b /wait /high /affinity 0F out\bench\benchmark-std_copy
  5. Prerequisites for STL development

    main

    To participate in the development of the STL (building from source), you must install the following components via the Visual Studio Installer:

    Visual Studio Components

    • VS 2026 Insiders: You must use the Insiders IDE and the Preview build tools.
    • Workload: "Desktop development with C++".
    • Minimum Components:
      • "MSVC Build Tools for x64/x86 (Preview)"
      • "C++ CMake tools for Windows"
      • "MSVC AddressSanitizer"
      • "Windows 11 SDK (10.0.28000)" or later
      • "C++ Clang tools for Windows (22.1.3 - x64/x86)"
      • Optional: "MSVC Build Tools for ARM64/ARM64EC (Preview)" (required for architecture-sensitive code like <atomic>).

    Other Software

    • Python: Version 3.14.6 or later. If you want to use the python command directly, ensure you select "Add python.exe to PATH" during installation.
  6. Run tests with Address Sanitizer (ASan)

    main

    To run tests with both the test code and the STL headers instrumented with ASan, you must build the STL itself with ASan enabled using the STL_ASAN_BUILD=ON flag.

    Because ASan-instrumented STL binaries require the executable to be instrumented as well, you must filter the test run to only include ASan configurations by passing -Dtags=ASAN to stl-lit.py.

    # Build STL with ASan
    cmake --preset x64 -DSTL_ASAN_BUILD=ON
    cmake --build --preset x64
    
    # Run only ASan-tagged tests
    python tests\utils\stl-lit\stl-lit.py ..\..\tests\std\tests\VSO_0000000_vector_algorithms -Dtags=ASAN -v
  7. Build the STL using the Visual Studio IDE

    main

    Follow these steps to build the repository using the Visual Studio IDE:

    1. Open Visual Studio and select "Clone a repository".
    2. Use https://github.com/microsoft/STL.git as the repository location and choose a local path.
    3. Once cloned, go to File > Open > Folder... and select the cloned repository folder.
    4. Use the IDE's dropdown menu to select your target architecture (x64 is recommended for general development).
    5. Select Build > Build All.
    git clone https://github.com/microsoft/STL.git
  8. How to consume the built STL library

    main

    To use the built STL in your own projects, you need to configure your build system to find the headers and libraries.

    Key Directories:

    • Headers: Search the inc directory.
    • Libraries: Search the lib/{architecture} directory.
    • DLLs (if using DLL flavor): Located in bin/{architecture}.

    Important Notes:

    • Library Naming: The names of the import and static libraries match those shipped with MSVC. Standard compiler switches like /MD, /MDd, /MT, or /MTd will work without modification.
    • DLL Suffix: DLLs generated by this CMake build system have a default suffix of _oss (e.g., msvcp140d_oss.dll). This prevents conflicts with standard MSVC DLLs in System32 and ensures your application uses the specific version it was built with.
    • Environment Setup: The build process generates a set_environment.bat script in the output directory. Running this script automatically updates your INCLUDE, LIB, and PATH environment variables to point to the correct built headers and libraries.
  9. Debug individual STL tests

    main

    To debug a failing test, first build the STL and run the specific test directory using stl-lit.py -v.

    To enable debugging with Visual Studio, identify the build command printed by stl-lit.py and add the /Zi and /Fdb<name>.pdb flags before the "-link" option. This generates debug symbols. You can then launch the resulting executable and the source .cpp file directly in devenv (Visual Studio).

    Note on DLLs: If testing configurations with dynamic linkage (/MD or /MDd), you may encounter missing msvcp140_oss.dll errors. To fix this, add the STL build's out/bin directory to your PATH environment variable.

    # 1. Build and run tests with verbosity
    C:\Dev\STL\out\x64> ninja
    C:\Dev\STL\out\x64> python tests\utils\stl-lit\stl-lit.py -v C:\Dev\STL\tests\std\tests\GH_XXXX_meow
    
    # 2. Add debug symbols to the build command (example)
    cl "C:\Dev\STL\tests\std\tests\GH_XXXX_meow\test.cpp" [...] "-FeC:\Dev\STL\out\x64\tests\std\tests\GH_XXXX_meow\Output\02\GH_XXXX_meow.exe" /Zi /Fdbark.pdb "-link" [...]
    
    # 3. Launch in Visual Studio
    devenv "C:\Dev\STL\out\x64\tests\std\tests\GH_XXXX_meow\Output\02\GH_XXXX_meow.exe" "C:\Dev\STL\tests\std\tests\GH_XXXX_meow\test.cpp"
    
    # 4. Fix DLL path if using /MD or /MDd
    set PATH=C:\Dev\STL\out\x64\out\bin\amd64;%PATH%
  10. How to report STL bugs

    main

    Bugs should be reported directly to the STL maintainers via GitHub issues.

    Bug Reporting Rules

    • Scope: Only report actual STL implementation bugs here.
    • Verification: Ensure the issue is an implementation bug and not just undefined behavior or surprising-yet-Standard behavior. Use Compiler Explorer to compare implementations.
    • Test Case: Provide a self-contained command-line test case. This should ideally include only STL and CRT headers. If you must include other libraries to reproduce the bug, ensure you have extracted the minimal logic required to trigger it.
    • Title Format: Use the pattern <header_name>: Short description of your issue.
      • Example: <type_traits>: is_cute should be true for enum class FluffyKittens
  11. Build the STL using the Command Prompt

    main

    To build the Microsoft STL from source using the command line, follow these steps to clone the repository and use CMake presets for different architectures.

    Prerequisites:

    • Git installed.
    • Visual Studio installed (ensure you know the installation path for vcvarsall.bat).
    • CMake installed.

    Initial Setup:

    git clone https://github.com/microsoft/STL.git --recurse-submodules
    pushd STL

    Building for specific targets: Note: If Visual Studio is in a non-default location, adjust the path to vcvarsall.bat accordingly. The following examples use the default path for Visual Studio 18 Insiders.

    TargetArchitectureCMake Presetvcvarsall.bat Argument
    x64 (Recommended)x64x64x64 -vcvars_ver=preview
    x86x86x86x86 -vcvars_ver=preview
    ARM64ARM64ARM64x64_arm64 -vcvars_ver=preview
    ARM64ECARM64ECARM64ECx64_arm64 -vcvars_ver=preview
    # Example: Building x64 target
    pushd "%ProgramFiles%\Microsoft Visual Studio\18\Insiders\VC\Auxiliary\Build"
    vcvarsall.bat x64 -vcvars_ver=preview
    popd
    cmake --preset x64
    cmake --build --preset x64
  12. Add and run benchmarks

    main

    Benchmarks are located in the benchmarks directory and use the Google Benchmark library.

    To add a new benchmark:

    1. Create a new file in benchmarks/src.
    2. Add add_benchmark(<name> <source_file>) to benchmarks/CMakeLists.txt.

    To run benchmarks:

    1. Build the STL.
    2. Build the benchmarks using the STL_BINARY_DIR pointing to your STL build output.
    3. Execute the benchmark binary. Use --benchmark_out=<file> and --benchmark_out_format=csv to export results to a spreadsheet-compatible format.
    # 1. Build STL
    cmake --preset x64
    cmake --build --preset x64
    
    # 2. Build benchmarks
    cmake -B out\bench -S benchmarks -G Ninja -DSTL_BINARY_DIR=out\x64
    cmake --build out\bench
    
    # 3. Run and export results
    out\bench\benchmark-std_copy --benchmark_out=benchmark-std_copy-results.csv --benchmark_out_format=csv