emk / rust-musl-builder

Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.
Apache License 2.0
1.54k stars 193 forks source link

Resulting binary errors with "No such file or directory" #77

Closed cmdln closed 4 years ago

cmdln commented 5 years ago

What did you try to do?

I built a project that I've built countless times before but recently ported from Iron over to Actix Web. The project continues to build without error. I tried to run the binary from target/x86_64-unknown-linux-musl. I am currently building with the 1.35.0 image. I am also building natively with 1.36.0 for Linux without any issues.

What happened?

When I try to run the resulting binary, I get the error "No such file or directory". I ran file target/x86_64-unknown-linux-musl/release/my-binary, and got

target/x86_64-unknown-linux-musl/release/portcullis: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld64.so.1, BuildID[sha1]=6a2eb424dd005a8140226c1598117c812c2246c1, with debug_info, not stripped

Maybe it isn't correctly statically compiling?

What did you hope to happen?

That the binary would actually run.

Does ./test-image work?

Yes. OK. ALL TESTS PASSED.

Additional information

The openssl crate now has a "vendored" feature, I am using that in my Cargo.toml. Is it possible that is causing or exacerbating the problem?

emk commented 5 years ago

Maybe it isn't correctly statically compiling?

Yes, static linking is failing. This is probably caused by some specific crate that you're including, probably one that links to a C library. Try running:

ldd target/x86_64-unknown-linux-musl/release/portcullis

This will print out any dynamic dependencies. Once you have a list, then things get fun: It will be necessary to figure out how to link those libraries statically. :-(

cmdln commented 5 years ago

I will try that. The fact that the rust openssl package now has the vendored feature and at least one or two other crates I've seen seem to be going this route makes this a little less surprising. If it is one of the crates that does this, I will try to open an issue in their repo about better support for static compilation.

MartenCatcher commented 5 years ago

@emk thank you a lot! Your advice helped me find a root cause of a problem. I used С-library wrapped with Rust for a first time and forgot about static/dynamic libraries linking. It's not a solution but a first step and a right direction.

emk commented 4 years ago

Great! Sorry for the slow response, and I'm happy everybody figured this out.