Skip to content

fix(msvc): let the archiver find Ole32.lib and other objects given by name - #2114

Merged
NobodyXu merged 1 commit into
rust-lang:mainfrom
waltuov:fix/msvc-relative-object-search
Nov 23, 2026
Merged

NobodyXu merged 1 commit into
rust-lang:mainfrom
waltuov:fix/msvc-relative-object-search

Conversation

@waltuov

@waltuov waltuov commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Since cc 1.9.0, build scripts that pass a system library by name through Build::object fail on MSVC targets:

cc::Build::new()
    .file("csrc/com.c")
    .object("Ole32.lib")
    .compile("objsearch");
LINK : fatal error LNK1181: cannot open input file 'C:\...\objsearch\Ole32.lib'

directxtex 1.3.0 does this (build.rs), and JorisJonkers-dev/ttwright-rs holds cc below 1.9 because of it (#34, #48). escapi 4.0.0 passes eight system libraries the same way (build.rs).

Up to 1.8.0, cc ran lib.exe in the build script's current directory, the package folder, and passed such a name as given. lib.exe and llvm-lib look for an input in the current directory and then along LIB, which cc sets when it finds the MSVC tools, so they found the SDK's Ole32.lib and added its import members to the library. #2033 runs the archiver in the output directory, so member names don't contain the build path, and passes a relative object from elsewhere by its absolute path so the archiver still finds it. For a name like Ole32.lib that path doesn't exist.

This makes a relative object absolute only when it exists from the current directory. Any other name is passed as given, and the archiver looks for it in the output directory and then along LIB. Compared with 1.8.0, the only new place is the output directory, searched before LIB. The rest of #2033 stays: objects cc compiles are passed relative to the output directory, an existing relative object is passed by its absolute path, and ARFLAGS, ar_flag and arguments in AR keep the current directory. ar, llvm-ar and libtool are unchanged.

cargo:rustc-link-lib=ole32 is the usual way to link a system library, but lib.exe takes existing libraries as inputs and these builds worked up to 1.8.0. With lib.exe and llvm-lib, only builds that fail today change: since 1.9.0 such a name reached the archiver as a path to a missing file.

Testing:

  • msvc_archive_passes_unknown_names_as_given checks that Ole32.lib and missing/foo.obj reach the archiver as given while it runs in OUT_DIR. It fails on main.
  • The two tests from fix(msvc): pass objects to the archiver relative to the output directory #2033 now pass Cargo.toml, which exists from the current directory, so they still check that an existing relative object is passed by its absolute path.
  • Tested on Windows with the MSVC 14.52 lib.exe and the build script above. cc 1.8.0 builds, 1.9.0 and 1.11.3 fail with LNK1181, and this branch builds. The binary calls CoInitializeEx through the library without a rustc-link-lib=ole32 line. lib.exe /list shows 1d1ea73a2512983e-com.o and the ole32.dll import members, so the archive still holds no build path.
  • lib.exe and LLVM's lib driver (run as rust-lld -flavor link /lib) both find a bare name and a relative path with a folder along LIB.
  • Linux and macOS were not run here.

…by name

Since cc runs `lib.exe` and `llvm-lib` in the output directory, a relative
object that isn't under it is passed by its absolute path from the current
directory. A name that only the archiver can find, such as
`.object("Ole32.lib")`, which `lib.exe` used to find along `LIB`, then
became a path in the build script's folder that doesn't exist, and the
archiver failed with LNK1181.

Make a relative object absolute only when it exists from the current
directory. Any other name is passed as given, and the archiver looks for it
in the output directory and then along `LIB`.

@NobodyXu NobodyXu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@NobodyXu
NobodyXu merged commit 5d7d4a7 into rust-lang:main Nov 23, 2026
85 checks passed
@waltuov
waltuov deleted the fix/msvc-relative-object-search branch November 23, 2026 15:13
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.

2 participants