Repository: rust
Issue: Windows linker error when importing a crate that has external symbols
Labels: A-linkage,O-windows,C-bug

When importing a crate that has external symbols, but only actually calling functions that do not use any externals, Linux compilation is successful, while Windows fails (both MSVC and GNU).

Repository for repro: https://github.com/radu-matei/rust-windows-linker-external-symbols-issue

I have the following crate that exports two functions: one which uses external symbols, one which doesn't.

```rust
// crate_with_external_symbols

pub fn test_not_using_extern() {}

pub fn abc() {
    let _ = unsafe { foo(1, 2) };
}

extern "C" {
    fn foo(x: i32, y: i32) -> i32;
}
```

Then, taking the crate as a dependency in another binary, and using the function that does not import any external symbols:

```rust
use crate_with_external_symbols::test_not_using_extern;

fn main() {
    test_not_using_extern();
    println!("Hello, world!");
}
```

On Linux (and IIRC on macOS as well), everything works fine, the program compiles and runs properly, given that the external symbols from the crate are not actually called at any point in the program.

On Windows, however, this fails to compile (regardless of building incrementally or not, which is why I suspect this is not a duplicate of #45929), resulting in a linker error using both the MSVC and the GNU toolchains (see backtrace below for the full log):

```
# MSVC
error LNK2019: unresolved external symbol foo referenced in function _ZN27crate_with_external_symbols3abc17hfc584f18839f815eE

# GNU
undefined reference to `foo'
```

Note: this is not a duplicate of #45929, which only fails for incremental builds, nor is it a duplicate of #71720, which only happens when using LTO.



### Meta

```
➜ rustc --version --verbose
rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-pc-windows-msvc
release: 1.52.1
LLVM version: 12.0.0
```


<details><summary>Backtrace</summary>
<p>

```
➜ cargo build --release
   Compiling rust-windows-linker-external-symbols-issue v0.1.0 (Z:\home\radu\projects\src\github.com\misc\rust-windows-linker-external-symbols-issue)
error: linking with `link.exe` failed: exit code: 1120
  |
  = note: "D:\\VS\\VS19\\VC\\Tools\\MSVC\\14.29.30037\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue.rust_windows_linker_external_symbols_issue.8qm5rhon-cgu.0.rcgu.o" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue.rust_windows_linker_external_symbols_issue.8qm5rhon-cgu.1.rcgu.o" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue.rust_windows_linker_external_symbols_issue.8qm5rhon-cgu.2.rcgu.o" "/OUT:Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue.exe" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue.4wa3aibiqoh6uxwt.rcgu.o" "/OPT:REF,ICF" "/DEBUG" "/NATVIS:C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "/LIBPATH:Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps" "/LIBPATH:C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "\\\\wsl$\\Ubuntu-20.04\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\libcrate_with_external_symbols-1dff5dd8370c4e1e.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-68b25ab3829cbbae.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-dd86b2a9657509c7.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_demangle-a32547dedd895cf6.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libhashbrown-2d29ef3bbf523eaa.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_alloc-2a226fea424b96ba.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-e28afa8739a1fa1c.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-5e97a47829d8661c.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-c7a4b246b1481113.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-2b5ec6322b49dd45.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-c1df47dd61fa6cd5.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-3cedc7817d8eb468.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-c115f0a110b00510.rlib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"

  = note: libcrate_with_external_symbols-1dff5dd8370c4e1e.rlib(crate_with_external_symbols-1dff5dd8370c4e1e.crate_with_external_symbols.759muw3a-cgu.0.rcgu.o) : 
error LNK2019: unresolved external symbol foo referenced in function _ZN27crate_with_external_symbols3abc17hfc584f18839f815eE

          Z:\home\radu\projects\src\github.com\misc\rust-windows-linker-external-symbols-issue\target\release\deps\rust_windows_linker_external_symbols_issue.exe : 
fatal error LNK1120: 1 unresolved externals



# building with the GNU toolchain results in the same error:

➜ cargo +stable-x86_64-pc-windows-gnu build --release
   Compiling crate-with-external-symbols v0.1.0 (Z:\home\radu\projects\src\github.com\misc\rust-windows-linker-external-symbols-issue\crate\crate-with-external-symbols)
   Compiling rust-windows-linker-external-symbols-issue v0.1.0 (Z:\home\radu\projects\src\github.com\misc\rust-windows-linker-external-symbols-issue)
error: linking with `x86_64-w64-mingw32-gcc` failed: exit code: 1
  |
  = note: "x86_64-w64-mingw32-gcc" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "-nostartfiles" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\self-contained\\crt2.o" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "-L" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-L" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\self-contained" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue-7411ee82aa505e47.rust_windows_linker_external_symbols_issue.2hciqv4d-cgu.0.rcgu.o" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue-7411ee82aa505e47.rust_windows_linker_external_symbols_issue.2hciqv4d-cgu.1.rcgu.o" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue-7411ee82aa505e47.rust_windows_linker_external_symbols_issue.2hciqv4d-cgu.2.rcgu.o" "-o" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue-7411ee82aa505e47.exe" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\rust_windows_linker_external_symbols_issue-7411ee82aa505e47.2jhj2xj0mxvtzlcl.rcgu.o" "-Wl,--gc-sections" "-nodefaultlibs" "-L" "Z:\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps" "-L" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-Wl,-Bstatic" "\\\\wsl$\\Ubuntu-20.04\\home\\radu\\projects\\src\\github.com\\misc\\rust-windows-linker-external-symbols-issue\\target\\release\\deps\\libcrate_with_external_symbols-e8b2ecdf14825a14.rlib" "-Wl,--start-group" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libstd-5c720e8db4b99e37.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libpanic_unwind-29283164da786e03.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libobject-1776be3a32adbb49.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libaddr2line-0892c1b32ffa2c4d.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libgimli-517a95b0a57b9e8e.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_demangle-82d48d88ac13ce1c.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libhashbrown-37de12e440fa77bc.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_alloc-7549ba16005c4b88.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libunwind-b131f4022158fa79.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcfg_if-bfa5ff0422f13358.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liblibc-454b6cdb3888a4db.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\liballoc-90ee5fe37d1109a4.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\librustc_std_workspace_core-f5802464b35b87c4.rlib" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcore-7ca46f1af2cdfb26.rlib" "-Wl,--end-group" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-13d041c714b5c5f4.rlib" "-Wl,-Bdynamic" "-ladvapi32" "-lws2_32" "-luserenv" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "C:\\Users\\root\\.rustup\\toolchains\\stable-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
  
= note: \\wsl$\Ubuntu-20.04\home\radu\projects\src\github.com\misc\rust-windows-linker-external-symbols-issue\target\release\deps\libcrate_with_external_symbols-e8b2ecdf14825a14.rlib(crate_with_external_symbols-e8b2ecdf14825a14.crate_with_external_symbols.etkck3m3-cgu.0.rcgu.o):crate_with_externa:(.text+0x1b): 

undefined reference to `foo'
```

</p>
</details>