Skip to content

Link against the runtime import library on Windows - #328

Merged
tamnd merged 1 commit into
mainfrom
compilerrt-import-lib
Sep 9, 2026
Merged

tamnd merged 1 commit into
mainfrom
compilerrt-import-lib

Conversation

@tamnd

@tamnd tamnd commented Sep 9, 2026

Copy link
Copy Markdown
Owner

Fixes #327.

mojo build on Windows stopped at the link step:

lld-link: error: C:\tmp\mojohome6/lib/KGENCompilerRTShared.dll: bad file type. Did you specify a DLL instead of an import library?
mojo.exe: error: failed to link executable

mojo-build.cpp put config.getCompilerRTPath() on the linker command line. That is lib/ plus whatever the target platform calls a shared library, so targeting Windows it is the DLL, and a COFF linker cannot read one. The list of names a Windows shared library defines lives in the import library beside it, and the DLL is what the loader picks up afterwards by the name recorded in the import.

getCompilerRTImportLibraryPath already existed and ObjectCompiler already used it. This was the one place left that named the DLL.

The error for a missing file now says which file, since "unable to locate Mojo CompilerRT library" with no path was not much help when the problem was that it was looking for the wrong one.

Verified by cross building a Windows mojo.exe, staging it with an lld-link.exe next to it and the runtime under lib/, and building a hello world on a Windows host:

C:\tmp\bld1> mojo build hello.mojo
BUILD_RC=0
C:\tmp\bld1> hello.exe
hello from a windows build
RUN_RC=0

That is the first Mojo program compiled to an executable on Windows by a Windows compiler. Before this change the same command got as far as the linker and no further.

The built binary imports KERNEL32.dll and KGENCompilerRTShared.dll and nothing else, and the runtime DLL has to be beside it to start, which is the packaging question in #319 rather than anything here.

@tamnd tamnd added this to the M6 Tier 2 and native hosting milestone Sep 9, 2026
@tamnd tamnd added area/compiler KGEN compiler, Mojo/lib and Mojo/tools area/build Bazel, toolchain, sysroot type/bug Something is broken priority/p1 Important. Needed for the milestone labels Sep 9, 2026
mojo build failed at the link step with "bad file type. Did you specify a DLL instead of an import library?". mojo-build.cpp puts getCompilerRTPath() on the linker command line, which is lib/ plus whatever the target calls a shared library, so targeting Windows it is the DLL. A COFF linker cannot read one. The list of names a Windows shared library defines is in the import library beside it, and that is the file a link needs.

getCompilerRTImportLibraryPath already exists and ObjectCompiler already uses it. This was the one place left that named the DLL.

The error when the file is missing now says which file it looked for, because "unable to locate Mojo CompilerRT library" with no path was not much to go on when the answer is that it was looking for the wrong one.
@tamnd
tamnd force-pushed the compilerrt-import-lib branch from 7ee929e to 17ad998 Compare September 9, 2026 07:44
@tamnd
tamnd merged commit 17ad998 into main Sep 9, 2026
7 checks passed
@tamnd
tamnd deleted the compilerrt-import-lib branch September 9, 2026 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/build Bazel, toolchain, sysroot area/compiler KGEN compiler, Mojo/lib and Mojo/tools priority/p1 Important. Needed for the milestone type/bug Something is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mojo build hands the linker the runtime DLL instead of its import library

1 participant