Making the linker happy when building a `*-sys` crate


For work I’m trying to wrap a 3rd party DLL so I can prototype something in Rust. I’ve done a lot of this sort of FFI stuff in the past, but it’s mainly been calling a Rust Shared Object/DLL from other languages. Now that I’m trying to go in the other direction for a non-trivial program I’ve started encountering linker errors when compiling the *-sys crate on Windows (the target platform).

In my foo-sys/Cargo.toml I’ve added a links = "foo" line to let cargo know this crate links to the foo system library. In foo-sys/ I’m also making sure to print out "cargo:rustc-link-lib=foo", which will presumably adds the equivalent of -lfoo to the linker args.

What I’m currently doing doesn’t seem to be working because when I try to run the foo-sys smoke test I’m getting linker errors. Are there any good example scripts where people have had to link to pre-installed DLLs dynamically on Windows when you can’t build from source?

Amusingly when I was trying this on my Linux laptop using cross and cross-compiling to i686-pc-windows-gnu, everything Just Worked :stuck_out_tongue_winking_eye:


Are you setting cargo:rustc-link-search=native={path}? (possibly indirectly via pkg_config crate and such).

If the paths are OK for -lfoo to work, then cargo:rustc-link-lib=foo should be enough.

One related gotcha that gets me is that Rust doesn’t actually link dependencies unless there’s extern crate foo_sys in dependent projects (having it in Cargo.toml is not enough).