# Can't statically link .lib file

**URL:** <https://users.rust-lang.org/t/cant-statically-link-lib-file/42405>\
**Category:** help\
**Created:** [May 11, 2020, 8:57am UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405 "2020-05-11T08:57:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon88867977](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@anon88867977](https://users.rust-lang.org/u/anon88867977)\
**Post date:** [May 11, 2020, 8:57am UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405/1 "2020-05-11T08:57:28Z")

</div>

Hi there, I'm trying to statically link .lib file on Windows.

[build.rs](https://github.com/DuckerMan/vigem/blob/master/build.rs)

When I'm trying to call function I got error:

````nohighlight
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.25.28610\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.11tcm5clyjcw4wr4.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\targeto" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.2mxt10hvrocsx4eq.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.30ao5qwrzwxal3vk.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.3fjaxvhjg1koz0zs.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.4d83ewdydfuenuy7.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.4m72kpgyxl87pkzx.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.4tyxmybuxezrmh03.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.4uf3tgnzes0ev5ci.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.c4468el6koyih47.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.njxrig3zh9tqmke.rcgu.o" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\examples\\xbox.x4n9bs8ihd4z8s0.rcgu.o" "/OUT:C:\\Users\\Ducke345z2vb9.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "/LIBPATH:C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\deps" "/LIBPATH:libs" "/LIBPATH:C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\deps\\libvigem-a0fde6e297922ac9.rlib" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\deps\\liblibloading-70313bacd7e21083.rlib" "C:\\Users\\DuckerMan\\Desktop\\RUST\\vigem\\target\\debug\\deps\\libwinapi-f629da6e18cebb27.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-bc2b04b7c9a44f97.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-73c12b6959d9c5fd.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libhashbrown-c41a2b552e68f0cc.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_alloc-e79a4701ffa7cbcc.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libbacktrace-9f3b0426ee0f39d1.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_demangle-31bd0c60452501f1.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-a9c788e4be0605b0.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-1999078cd5ceb0ce.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-5dd545b022f6be58.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-acd4590212ce9dae.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-87cb4dffff33f37a.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-81caf0281e0b427b.rlib" "C:\\Users\\DuckerMan\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-bb027328a052d4db.rlib" "VigemClient_x64.lib" "kernel32.lib" "user32.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
  = note: VigemClient_x64.lib(ViGEmClient.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance
          LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
          VigemClient_x64.lib(ViGEmClient.obj) : error LNK2001: unresolved external symbol __imp_SetupDiDestroyDeviceInfoList
          VigemClient_x64.lib(ViGEmClient.obj) : error LNK2001: unresolved external symbol __imp_SetupDiEnumDeviceInterfaces
          VigemClient_x64.lib(ViGEmClient.obj) : error LNK2001: unresolved external symbol __imp_SetupDiGetClassDevsW
          VigemClient_x64.lib(ViGEmClient.obj) : error LNK2001: unresolved external symbol __imp_SetupDiGetDeviceInterfaceDetailW
          C:\Users\DuckerMan\Desktop\RUST\vigem\target\debug\examples\xbox.exe : fatal error LNK1120: 4 unresolved externals ```
````

---

<div class="post-metadata">

**Author:** ![naim](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/naim/32/6661_2.png) [@naim](https://users.rust-lang.org/u/naim)\
**Post date:** [May 11, 2020, 9:42am UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405/2 "2020-05-11T09:42:35Z")

</div>

add to your build.rs

```rust
fn main() {
    println!("cargo:rustc-link-lib=setupapi");
}

```

According to the build script it seems like you are trying to link to `VigemClient_x64`, when that's actually the name of the lib that you are developing. I don't think that this is correct... You should probably remove the existing `rustc-link-lib` and `rustc-search-libs` from `build.rs`.

---

<div class="post-metadata">

**Author:** ![Michael-F-Bryan](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/michael-f-bryan/32/28749_2.png) [@Michael-F-Bryan](https://users.rust-lang.org/u/Michael-F-Bryan)\
**Post date:** [May 11, 2020, 9:49am UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405/3 "2020-05-11T09:49:37Z")

</div>

You may also want to check out [_Making \*-sys crates in Rust_](https://kornel.ski/rust-sys-crate). It goes through some of the other things you might want to do when linking to native libraries.

---

<div class="post-metadata">

**Author:** ![anon88867977](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@anon88867977](https://users.rust-lang.org/u/anon88867977)\
**Post date:** [May 11, 2020, 10:13am UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405/4 "2020-05-11T10:13:51Z")

</div>

Thanks for reply! I'm trying to link [this](https://github.com/ViGEm/ViGEmClient) lib, and its name is VigemClient

---

<div class="post-metadata">

**Author:** ![Michael-F-Bryan](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/michael-f-bryan/32/28749_2.png) [@Michael-F-Bryan](https://users.rust-lang.org/u/Michael-F-Bryan)\
**Post date:** [May 11, 2020, 10:25am UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405/5 "2020-05-11T10:25:20Z")

</div>

Looking at [this random issue](https://github.com/signal11/hidapi/issues/403) it seems like you need to link to `Setupapi.lib`. @naim's `println!("cargo:rustc-link-lib=setupapi")` suggestion should tell `rustc` to link it in.

---

<div class="post-metadata">

**Author:** ![anon88867977](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@anon88867977](https://users.rust-lang.org/u/anon88867977)\
**Post date:** [May 11, 2020, 10:30am UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405/6 "2020-05-11T10:30:54Z")

</div>

> [@Michael-F-Bryan](#):
>
> cargo:rustc-link-lib=setupapi

You are my life-saver! How did you find that I have to link setupapi?

---

<div class="post-metadata">

**Author:** ![Michael-F-Bryan](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/michael-f-bryan/32/28749_2.png) [@Michael-F-Bryan](https://users.rust-lang.org/u/Michael-F-Bryan)\
**Post date:** [May 11, 2020, 1:32pm UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405/7 "2020-05-11T13:32:35Z")

</div>

> [@anon88867977](#):
>
> VigemClient\_x64.lib(ViGEmClient.obj) : error LNK2001: unresolved external symbol \_\_imp\_SetupDiDestroyDeviceInfoList

When you run into linker errors, look out for something like this:

```rust
VigemClient_x64.lib(ViGEmClient.obj) : error LNK2001: unresolved external symbol __imp_SetupDiDestroyDeviceInfoList

```

The linker is saying _"I checked every library you've told me to link to and can't find the `__imp_SetupDiDestroyDeviceInfoList` function"_.

From there you can throw the symbol name into google and usually one of the search results will mention either the library it belongs to, or someone who's run into the same problem and been told they need to link to `setupapi.lib`.

The issue that did it for me was this:

> <https://github.com/signal11/hidapi/issues/403>
>
> Hi,
> i would like to build a static library for windows, but when i set the libr…ary from dynamic to static i get the following unresolved symbols when building hidtest:
> 
> \`\`\`
> 1\>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "\_\_imp\_SetupDiDestroyDeviceInfoList".
> 1\>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "\_\_imp\_SetupDiEnumDeviceInterfaces".
> 1\>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "\_\_imp\_SetupDiGetClassDevsA".
> 1\>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "\_\_imp\_SetupDiGetDeviceInterfaceDetailA".
> 1\>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "\_\_imp\_SetupDiEnumDeviceInfo".
> 1\>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "\_\_imp\_SetupDiGetDeviceRegistryPropertyA".
> \`\`\`
> What else am i missing??

In it they have a bunch of similar linker errors.

```rust
>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp_SetupDiDestroyDeviceInfoList".
1>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp_SetupDiEnumDeviceInterfaces".
1>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp_SetupDiGetClassDevsA".
1>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp_SetupDiGetDeviceInterfaceDetailA".
1>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp_SetupDiEnumDeviceInfo".
1>hidapi.lib(hid.obj) : error LNK2001: Nicht aufgelöstes externes Symbol "__imp_SetupDiGetDeviceRegistryPropertyA".

```

(I don't speak german, but the error message looks similar and "nicht aufgelöstes externes Symbol" probably means "no (could find) external symbol")

And the [first comment](https://github.com/signal11/hidapi/issues/403#issuecomment-409187515) says this:

> Add `Setupapi.lib` to your link libraries.

And because I've had to link to native libraries in the past, I knew the correct incantation for cargo is `println!("cargo:rustc-link-lib=setupapi")`.

Does that thought process all make sense?

---

<div class="post-metadata">

**Author:** ![anon88867977](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@anon88867977](https://users.rust-lang.org/u/anon88867977)\
**Post date:** [May 11, 2020, 1:34pm UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405/8 "2020-05-11T13:34:18Z")

</div>

Yep. Thanks for explanation 🙂

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 9, 2020, 1:34pm UTC](https://users.rust-lang.org/t/cant-statically-link-lib-file/42405/9 "2020-08-09T13:34:20Z")

</div>

Closed on August 9, 2020, 1:34pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
