Can't statically link .lib file

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

build.rs

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

  |
  = 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 ```
1 Like

add to your build.rs

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.

1 Like

You may also want to check out Making *-sys crates in Rust. It goes through some of the other things you might want to do when linking to native libraries.

Thanks for reply! I'm trying to link this lib, and its name is VigemClient

Looking at this random issue 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.

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

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

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:

In it they have a bunch of similar linker errors.

>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 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?

3 Likes

Yep. Thanks for explanation :slight_smile:

1 Like

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