0

There is a lib-sys for libvips on crates.io, however it uses pkg-config which searches the system for the library to link to dynamically, not statically.

I want to provide libvips with the final binary of my software in .dll or .exe along with it since the user should only install one executable with everything in it, C and Rust code.

Looking at the Rust book's FFI linking section, we can link lib.a files easily, but libvips is a huge complex C library that has releases for Windows in .dll and .exe format. I do not know how to link these Windows binaries of libvips to a Rust program statically.

Potentially, I could build it from source manually and link it manually, but the build process seems very complex and it uses scripts and Docker. I would have to replace those scripts with a build.rs of my own that did the same but that seems very hard to me since I'm a beginner at this. I know I would have to set rustc-link-search in build.rs, but I don't know how to compile the .a files for libvips. Rust book on this

My goal is to FFI into libvips from Rust. I am using Windows 10 and want to cargo build the project and have the Rust code and libvips in one distributable binary with no other dependencies.

edit-1: the vips-dev-w64-web-x.y.z.zip contains libvips.lib , libvips.dll.a files, pkgconfig .pc files, as well as the normal .dll and vips.exe. Can i use these .lib and .a files and if so exactly how? Will it link statically? Are these .pc files useful for my situation?

Fdrph
  • 1
  • 1
  • While static linking may appear to deliver a better deployment story, it comes at a price: [Potential Errors Passing CRT Objects Across DLL Boundaries](https://learn.microsoft.com/en-us/cpp/c-runtime-library/potential-errors-passing-crt-objects-across-dll-boundaries). If you are building a library and are not sure, link dynamically. – IInspectable Aug 12 '18 at 17:00
  • I believe your question is answered by the answers of [How do I specify the linker path in Rust?](https://stackoverflow.com/q/40833078/155423) and [Linking to a static lib compiled with MSVC](https://stackoverflow.com/q/30725579/155423). If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Aug 12 '18 at 17:27
  • That being said, I'd recommend submitting a pull request to vips-sys to allow optionally providing a static library instead of using `pkg-config`. – Shepmaster Aug 12 '18 at 17:29
  • @technosaurus: The issue is not about relocation. The issue is about passing objects allocated from one heap to another part of the application for cleanup, that uses a different heap. If you ship object code that statically links against the CRT, you cannot in general safely use that library in a DLL. – IInspectable Aug 13 '18 at 19:40

1 Answers1

0

Just ship the libvips bin area in your tree somewhere, add it to PATH on startup, and link dynamically. This is how electron apps and node.js packages that use libvips work. Static linking for complex libraries died a while ago, it's not worth trying.

As much as anything, it would be a potential licence violation. libvips is LGPL, so you MUST link dynamically (or include enough of your code to allow relinking), or your whole application becomes open source.

jcupitt
  • 10,213
  • 2
  • 23
  • 39
  • Would electron app unzip the bins to %appdata%? I think sharp (the libvips node lib) static links everything to the executable no? or does it get it from `resources/app.asar.unpacked` – Fdrph Aug 12 '18 at 23:52
  • 1
    *Static linking for complex libraries died a while ago* — this comment is in direct contradiction with Rust defaults (the language the question about) as well as other languages like Go. – Shepmaster Aug 12 '18 at 23:54
  • sharp is fully dynamic, I believe. It has a C++ layer built with MSVC which loads into the node runtime. This layer then in turn dynamically loads the libvips DLL, which is built in gcc in a cross-compiler from linux. – jcupitt Aug 13 '18 at 00:04