3

I am trying to cross-compile my Rust project on Mac OS to Linux using cargo build --target=x86_64-unknown-linux-musl.

  • I installed the binary for Linux + musl cross-compilation on mac using brew install FiloSottile/musl-cross/musl-cross as I would ideally want a standalone binary.
  • I also installed the target using rustup target add x86_64-unknown-linux-gnu
  • And I have the following in my .cargo/config:
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"

However, I ran into issues with 2 libraries - sqlite3 and pcap:

  = note: /usr/local/Cellar/musl-cross/0.9.8/libexec/bin/../lib/gcc/x86_64-linux-musl/6.4.0/../../../../x86_64-linux-musl/bin/ld: cannot find -lsqlite3
          /usr/local/Cellar/musl-cross/0.9.8/libexec/bin/../lib/gcc/x86_64-linux-musl/6.4.0/../../../../x86_64-linux-musl/bin/ld: cannot find -lpcap
          collect2: error: ld returned 1 exit status

I was able to solve the sqlite3 linker issue by adding features = ["bundled"] to my Cargo.toml file (similar to what is described here - https://users.rust-lang.org/t/linker-cannot-find-lsqlite3/23230/18) as that likely built it from source (which was great) but when I tried doing the same with pcap, it gave the following error:

the package `myProject` depends on `pcap`, with features: `bundled` but `pcap` does not have these features.

After reading somewhere I also tried (with low hopes) of providing the path to the installed libpcap on mac using RUSTPATH='-L/...' cargo build ... but that resulted, of course in undefined symbol errors.

Any ideas how can I get past this issue and cross compile my Rust project into a statically linked binary on macos to run on linux?

1 Answers1

1

Cross compilation does not magically take care of libraries. You cannot just say "I want musl" and have something take care of all your dependencies.

The error message you are seeing is telling you exactly this: it cannot find libsqlite3 and libpcap.

The reason the error for sqlite disappears is because your sqlite library has a bundled feature, which replaces the linking with a built-in sqlite client. This, however, also requests the pcap bundled feature, which does not exist.

You have two options:

  • If you do not mind the performance loss in the bundled sqlite client, change your feature definition to target the feature of the dependency requiring sqlite
  • If you want the raw library itself, you will have to compile it for musl

No matter what happens, you will need to cross-compile libpcap for musl with the default sysroot provided by your musl compiler. As this varies per library, you will need to consult the libpcap documentation. once you have done so, you should be able to use the -lpcap flag, and the error will resolve itself.

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66