Skip to content

[Clang/MSVC] An explicit -nostdlib project still receives mcpp-injected CRT DEFAULTLIB directives #1051

Done

Description

@dreamyloong2005

Repository: mcpp-community/mcpp

Duplicate check (2026-12-06): No exact duplicate found in the documented search scope; scoped new-issue candidate

Live-source check: the owning current-main source still contains the relevant pattern. This is a static check, not a new runtime reproduction of current main. Search results are bounded and do not prove that no duplicate exists.

Summary

On the Clang/MSVC-ABI path, mcpp adds an explicit CRT-selection compile flag, normally -fms-runtime-lib=dll. That flag also causes Clang to put /DEFAULTLIB:msvcrt.lib and /DEFAULTLIB:oldnames.lib directives into each project object. A project that explicitly requests -nostdlib at link time therefore still tries to load those libraries, even if its code uses no headers or runtime functions.

This is a mismatch between mcpp's compile-time default policy and the project's explicit library-free link intent. It is not a claim that Clang incorrectly processes -nostdlib: suppressing driver-added libraries is not the same as suppressing default-library directives already embedded in input objects.

Pinned environment and expected / actual

  • mcpp runtime version: 2026.12.4.1; private source baseline: 332fea767afbb04207bb3f2a1806b64be5da0ca4.
  • Actual driver: Clang 22.1.8, x86_64-pc-windows-msvc. Compiler SHA-256: 986af49c2d1eefeac324f724ff54597753d1053927fa5d58a37a457f578e1cf8.
  • Windows x86-64. Both host object inspection and a real-LPAC minimized link control were recorded.
  • The original integration fixture intentionally provides only an empty MSVC toolset-layout placeholder, not CRT libraries or an SDK payload. Its no-header/no-runtime source and explicit entry point are intended to make those libraries unnecessary; do not interpret the placeholder's reported version as a real installed toolset.

Expected: an exact project -nostdlib request should not be defeated by default-library directives introduced solely by mcpp's own CRT-selection word. Preserve the selected CRT ABI macros. Continue honoring directives explicitly supplied by other objects; this is not a request to disable all linker default-library processing.

Actual: once the earlier linker-discovery issue is separated out, the library-free link fails:

lld-link: error: could not open 'msvcrt.lib': no such file or directory
lld-link: error: could not open 'oldnames.lib': no such file or directory
clang++: error: linker command failed with exit code 1

Minimal mcpp intent

The relevant project configuration is:

[build]
ldflags = ["-nostdlib", "-Wl,/entry:mainCRTStartup", "-Wl,/subsystem:console"]

The no-header source defines mainCRTStartup directly and calls no CRT functions. In the observed scenario mcpp still put -fms-runtime-lib=dll on project compilation. A project that actually calls runtime functions will naturally still need to supply them; this report does not propose otherwise.

Isolated compiler A/B: directives change, ABI macros remain

The following source and compiler/inspection commands were run with the pinned actual driver. Both compiles require the same _MT and _DLL ABI definitions:

#if !defined(_MT) || !defined(_DLL)
#error CRT ABI macros lost
#endif
extern "C" void mainCRTStartup() {}
# A: mcpp's normal explicit dynamic-CRT word.
clang++ -c abi.cpp -fms-runtime-lib=dll -o dll.obj
llvm-readobj --coff-directives dll.obj
# Directive(s): /DEFAULTLIB:msvcrt.lib /DEFAULTLIB:oldnames.lib

# B: retain the CRT model and omit only its automatic library directives.
clang++ -c abi.cpp -fms-runtime-lib=dll -fms-omit-default-lib -o dll-omit.obj
llvm-readobj --coff-directives dll-omit.obj
# No DEFAULTLIB directives.

Both compiles succeeded, including the ABI macro checks. This is an object-code/flag proof, not by itself an end-to-end mcpp acceptance result.

A separate real-LPAC control used a no-runtime main-dll.obj compiled with the dynamic-CRT word and an already-correct supplied linker search prefix. Its recorded A command was:

Z:/bin/probe/clang++.exe main-dll.obj -nostdlib -fuse-ld=lld `
  -BZ:/bin/probe -Wl,/entry:mainCRTStartup `
  -Wl,/subsystem:console -o hello.exe

A failed with the two missing-library errors above. B kept the same object, tool leaves and policy and added only -Wl,/nodefaultlib; it succeeded. Staged input and executable hashes remained unchanged.

That blanket /NODEFAULTLIB switch was a diagnostic control only. It is not the proposed fix and was not added to the product or original fixture. It suppresses third-party directives too, which would hide behavior outside mcpp's own injected defaults. The native link A/B and the compiler A/B answer different questions; they must not be reported as one compile-and-link-only-variable experiment.

Source / driver boundary

The mcpp decision is owned by modules/toolchain-model/src/dialect.cppm, msvc_abi_crt_word, and consumed in src/build/flags.cppm, where the MSVC-ABI compile baseline is combined with parsed project link flags.

Pinned LLVM llvmorg-22.1.8 explains the observed behavior:

  • ToolChains/Clang.cpp, ProcessVSRuntimeLibrary: explicit -fms-runtime-lib selects CRT ABI definitions and dependent-library directives. -fms-omit-default-lib retains those ABI definitions, defines _VC_NODEFAULTLIB, and withholds the automatic CRT/oldnames directives.
  • ToolChains/MSVC.cpp: -nostdlib withholds driver-added defaults/runtime libraries; it does not remove object-level directives.

Thus simply removing -fms-runtime-lib would lose mcpp's explicit ABI policy, while globally adding /NODEFAULTLIB would suppress too much.

Local patch strategy and regression boundary

A focused mcpp.toolchain.msvc_runtime_policy leaf takes the already parsed project link words and enables omission only for Clang targeting the MSVC ABI with an exact -nostdlib word. Project C and C++ compilation retain the selected -fms-runtime-lib and add -fms-omit-default-lib.

The patch does not:

  • add /NODEFAULTLIB or another link-wide default-library suppression;
  • rewrite third-party object directives;
  • apply this project-only omission to build-contract, build-program or std-module compilation;
  • infer it from -nostdlib++, -nodefaultlibs, -Wl,-nostdlib, a substring, or a different compiler/ABI.

Three helper cases and three project/scope flag cases passed in actual selected mcpp test runs. The independent real-leaf probe checked nine individual conditions; the compiler A/B above was also executed. Across this and the linker-discovery issue, the final selected Windows suite reported 43 passed, 0 failed and 10 explicitly skipped; the 13 new cases all passed. This is not the full upstream suite or native cross-platform validation.

The original frozen-Agent Debug/Release executable × default/custom-home four-cell integration gate subsequently passed with the combined local fixes, under its original compile/link/pack/execute/export assertions. The Debug/Release dimension selects the Agent executable; the fixture itself always packs the release profile, not four distinct CRT or project-profile combinations. That four-cell result is not an issue-only A/B, and retained provider warnings are not claimed fixed. A supplemental named-module cold/warm check covered only one Debug/default-home cell. The recorded object-code ABI assertion specifically tests dynamic CRT _MT/_DLL; it is not exhaustive validation of every static/debug CRT configuration, external library or SDK version.

Activity

  1. speak-agent commented on Dec 6, 2026

    @speak-agent
    Member

    Addressed in 2026.12.7.1 (#1054) as a statement, not an inference. mcpp does not read -nostdlib out of raw flags to change the CRT model; the build is unchanged. On the MSVC ABI a -nostdlib in [build] ldflags is now noted once (build/msvc-nostdlib): each object still names the default libraries of the CRT model, and docs/20 ("On the MSVC runtime") states how to choose the model. A Windows e2e case asserts the notice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions