Skip to content

Fail to link i686 staticlib with MSVC #36097

Closed
@upsuper

Description

@upsuper

See my test repo: upsuper/rust-msvc-link-test

  • Build the crate with i686-pc-windows-msvc toolchain
  • Build the C++ file with cl.exe from MSVC 2015
  • Link them together via link.exe from MSVC 2015

(The 32bit MSVC 2015 toolchain locates at "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64_x86" for me)

It shows linker error like:

test.lib(test.0.o) : error LNK2019: unresolved external symbol __imp__?after@nsCSSPseudoElements@@2PAVnsICSSPseudoElement@@A referenced in function _test
test.lib(test.0.o) : error LNK2019: unresolved external symbol _?before@nsCSSPseudoElements@@2PAVnsICSSPseudoElement@@A referenced in function _test

Using dumpbin -symbols to analyse main.obj and test.lib shows that, in main.obj, the symbols are

008 00000000 SECT3  notype       External     | ?after@nsCSSPseudoElements@@2PAVnsICSSPseudoElement@@A (public: static class nsICSSPseudoElement * nsCSSPseudoElements::after)
009 00000004 SECT3  notype       External     | ?before@nsCSSPseudoElements@@2PAVnsICSSPseudoElement@@A (public: static class nsICSSPseudoElement * nsCSSPseudoElements::before)

however, in test.lib generated by rustc, the import symbols are

02A 00000000 UNDEF  notype       External     | __imp__?after@nsCSSPseudoElements@@2PAVnsICSSPseudoElement@@A
02D 00000000 UNDEF  notype       External     | _?before@nsCSSPseudoElements@@2PAVnsICSSPseudoElement@@A

Thus the mismatch.

It's even more interesting that the symbol from nested crate is different from the crate of the lib, even though they are both extern symbols.

This issue doesn't show up for 64bit build with proper link_name. The corresponding symbols in rust staticlib with x86_64 toolchain would be:

A15B 00000000 UNDEF  notype       External     | __imp_?after@nsCSSPseudoElements@@2PEAVnsICSSPseudoElement@@EA (__declspec(dllimport) public: static class nsICSSPseudoElement * nsCSSPseudoElements::after)
A15C 00000000 UNDEF  notype       External     | __imp_?before@nsCSSPseudoElements@@2PEAVnsICSSPseudoElement@@EA (__declspec(dllimport) public: static class nsICSSPseudoElement * nsCSSPseudoElements::before)

It could either because of the different prefix (__imp_ vs. __imp__ and _) or the different link type? But the linker also complains about dllimport using on static link symbols for 64bit build.

cc @alexcrichton

Metadata

Metadata

Assignees

No one assigned

    Labels

    O-windows-msvcToolchain: MSVC, Operating system: Windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions