Skip to content

Windows: printf and the rest of stdio come from the runtime - #843

Merged
ASDAlexander77 merged 1 commit into
mainfrom
runtime-stdio-definitions
Nov 13, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
runtime-stdio-definitions

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Windows: printf and the rest of stdio come from the runtime

This change was pushed to x86-stdcall-printf after #842 had been merged, so it never reached main. It is the same commit, cherry-picked onto main.

The UCRT has printf, sprintf_s, scanf and the rest of stdio only as inline functions in its headers. Code tslang compiles never sees those headers, so a program calling one (a @varargs declare function printf, or the sprintf_s a number's conversion to text uses) needed legacy_stdio_definitions.lib in its link line. On x86 the CRT defines none of them; on x64 libucrt happens to define printf alone.

TypeScriptAsyncRuntime, which every Windows program links, now provides them:

  • lib/TypeScriptAsyncRuntime/StdioDefinitions.cpp takes the address of about 60 functions (the printf and scanf families, narrow and wide, with the _s and _snprintf variants). The compiler then emits the header's own definitions into the runtime, as the same selectany COMDATs a C or C++ caller gets. A program whose own objects already have copies links as before. On Linux the file compiles to nothing.
  • The file is added to both runtime builds: the in-tree one and tslang/runtime, which builds the x86 runtime.
  • legacy_stdio_definitions is removed from exe.cpp and the test runner's link list.
  • The CMake template and the docs/how samples go back to their original link lists, undoing 5c1b572, which had added legacy_stdio_definitions to them.

The definitions are compiled from the UCRT headers when the runtime is built. No MSVC static library is copied into it.

Testing

  • Both runtime archives (x64 and x86) export printf, sprintf_s, scanf, swprintf and the rest.
  • The printf and varargs tests pass, 88 of 88 on x64 and x86, with nothing extra in any link line.
  • A project generated by tslang --cmake, unchanged, whose TypeScript calls printf, links against the new runtime and runs. Against the old runtime it failed with unresolved external symbol printf.
  • Two mixed cases link with no duplicate-symbol error: C++ and TypeScript both calling printf, and C++ calling snprintf while the runtime's file is pulled in.
  • Full Windows suite: the 17 known x86 @cpp failures, nothing new. Linux: the build and the 63 related tests pass.

🤖 Generated with Claude Code

The UCRT has printf, sprintf_s, scanf and the rest of stdio only as inline
functions in its headers, which code tslang compiles never sees; a program
calling one needed legacy_stdio_definitions.lib in its link line. The
runtime every program links (TypeScriptAsyncRuntime) now carries them:
StdioDefinitions.cpp takes the address of each, so the compiler emits the
header's own definitions there, as the same `selectany` COMDATs a C or C++
caller holds - a program with copies of its own links as it did.

legacy_stdio_definitions is gone from exe.cpp and the test runner, and the
CMake template and docs samples no longer need it (reverts 5c1b572).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 878c7d6 into main Nov 13, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the runtime-stdio-definitions branch November 13, 2026 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant