How to include a static .lib into a lib crate so that binary crates that depend on the lib crate don't need to static .lib?

I have a project that must use a static .lib on windows. I have written a lib crate that interfaces with that .lib. I now have written another rust program that depends on the rust lib crate. When I try to build it I get an error that the static lib cannot be found. I thought that the whole point of a static library was that it is built into the crate and you don't need to continue to carry the static library around. For example in the C++ world if I statically link a library into a binary, I can just hand someone the binary and they can run it, even on another machine if the environments are close enough, they don't need the originally static library. But I can't seem to get this behavior in rust.

Project Structure looks like

  • Name1

  • Name2
    -Cargo.toml ( has Name1 as dependency)

This all works if I tell Name2 about the static .lib so that it too can link against it, but I would like to hide this dependency away in the Rust lib crate Name1 so that any future dependencies don't have to know about the C static library and continue to pull that library around for everyone in the world to have to link against it. How do I do this?

Yes, you can.

If you link static library with binary then you don't need to ship that static library. Both in C/C++ and Rust.

If you link static library with another binary then you need to ship both libraries and link them both. Both in C/C++ and Rust.

Now your question is turned intothis:
Rust behaves precisely and exactly like C and C++ behaved for more than half-century, why is that?

Does that question even need answering? Rust behaves like C and C++ exactly because it's what people should expect. The fact that you don't know how static libraries work in C or C++ is not much of an excuse.

You can use the exact same mechanism that you would use in C and C++. There are bazillion such tools: pkg-config, libtool and many others.

They all are, of course, have different warts and limitations, but that's not something Rust can fix: C and C++ behave like C and C++ always behaved, even if you use them from Rust.

then you must have a broken build script for your lib crate. you need to correctly set the necessary linker argument in the build script so that the linker knows what libraries to link and where to search for them. see:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.