Windows: Static libraries can't be linked as files by libtool/MinGW due to .lib extension #69904
Labels
A-linkage
Area: linking into static, shared libraries and binaries
C-bug
Category: This is a bug.
O-windows
Operating system: Windows
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Consider a C++ project using common build system tooling (autoconf, automake, gcc, libtool, pkg-conf etc.), that wants to statically link in a Rust library (which it will interact with via a C FFI). The project wants to target both Unix and Windows, so leverages MinGW for compiling C++ on Windows.
libtool follows the MinGW/GCC convention of static libraries ending in
.a
, not the MSVC convention of ending in.lib
. libtool does support linking libraries withlibfoo.a
andfoo.lib
naming patterns if the-Lpath/to/foo/ -lfoo
option pattern is used. However, if the library file is passed directly as an argument (which is a very common pattern in automake build systems when linking subunits together), libtool only treats.a
files as libraries;.lib
files are treated as unknown options, and moved to earlier in the eventual linker command. This in turn causes obscure linker errors due to a reordering of the library arguments.The root cause is that
rustc
unconditionally generates static libraries with.lib
extensions for all Windows compiler environments, instead of using.lib
for MSVC environments and.a
for MinGW environments. This behaviour was introduced in Rust 1.8.0 (previously.a
was used unconditionally for all static libraries - #29508).Possible workarounds without changes to
rustc
:-lfoo
option for linking.foo.lib
tolibfoo.a
after Rust compilation.Possible solutions:
rustc
matches users' default expectations.--out-dir
option for cargo, but changing the name of the artifact instead of the location.This issue is effectively a re-opening of #43749, as recommended in #43749 (comment).
The text was updated successfully, but these errors were encountered: