Search code examples
rustdependenciesrust-cargo

Rust packaged in .deb


I want to create a .deb package out of my Rust code. It consists of a library (crate) that is used by another library crate and a binary crate. So something like

- core_lib
- pam_lib (uses core_lib)
- cli_binary (uses core_lib)

Question 1

The issue is resolving dependencies, of course :-) I have tried two approaches:

Dynamic linking

When compiling the code, I can force where it should expect shared libraries to be found, as cargo rustc -r -- -C link-args='-Wl,-rpath,/usr/lib/x86_64-linux-gnu/mylib'. However, when I do this and dpkg build the package, lintian returns an error: custom-library-search-path.

Adding to ldconfig

I can also add a new config file to /etc/ld.so.conf.d/ and set a path to my /usr/lib/x86_64-linux-gnu/mylib dir, and execute sudo ldconfig in a postinst script to update the values. But is this the right way to go? I wouldn't like to follow any antipatterns.

Question 2

The other issue I'm having with these dependencies is that when I compile either of the 'final' binaries, it also compiles the core_lib in the target/release folder. So far, so good. But libcore_lib.so is not the same when I compile pam_lib or cli_binary (even byte size is somewhat different), and I cannot use them interchangeably. Not even when I compile core_lib independently and use that artifact; running either binary returns unknown symbol followed by a reference to a function from core_lib.

One possible solution would be to copy libcore_lib.so twice to different dirs (once for each binary from their own target/release dir), but since it is supposed to be a shared reference, that seems to be outright... stupid.

Any ideas, please?

Core library Cargo.toml:

[package]
name = "core-lib"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["dylib"]

[dependencies]
...

and binaries Cargo.toml:

[package]
name = "cli-binary"
version = "0.1.0"
edition = "2021"

[dependencies]
core-lib = { path = "../../Rust core/core-lib" }
...

Solution

  • Okay, the dumb part was... me :-)

    I was also browsing Rust forum and stumbled upon a topic that resembled mine - and sure enough, when I changed the core_lib type from dylib to lib, it all started working. I just imposed a lot of headache upon me by using dylib for no real reason (switched to that long time ago for reasons I no longer remember... sigh).

    Anyway, hope it helps someone else, too.