Build in "external libraries"

I try to compile app without need to install other dependieces on host so all libraries used in app shoul be static.
How to build release without using shared libraries?

$ cargo build --release 
   Compiling core v0.1.0 (/home/marcin/projects/rust/rust-nms/core)
   Compiling api v0.1.0 (/home/marcin/projects/rust/rust-nms/api)
   Compiling web v0.1.0 (/home/marcin/projects/rust/rust-nms/web)

    Finished release [optimized] target(s) in 4.46s
$ ldd target/release/nms (0x00007ffe127ff000) => /lib/x86_64-linux-gnu/ (0x00007fc42e337000) => /lib/x86_64-linux-gnu/ (0x00007fc42e31c000) => /lib/x86_64-linux-gnu/ (0x00007fc42e2f9000) => /lib/x86_64-linux-gnu/ (0x00007fc42e1aa000) => /lib/x86_64-linux-gnu/ (0x00007fc42e1a4000) => /lib/x86_64-linux-gnu/ (0x00007fc42dfb2000)
        /lib64/ (0x00007fc42e5d1000) => /lib/x86_64-linux-gnu/ (0x00007fc42df1e000) => /lib/x86_64-linux-gnu/ (0x00007fc42dc32000) => /lib/x86_64-linux-gnu/ (0x00007fc42dbe5000) => /lib/x86_64-linux-gnu/ (0x00007fc42db8f000) => /lib/x86_64-linux-gnu/ (0x00007fc42dab2000) => /lib/x86_64-linux-gnu/ (0x00007fc42da7f000) => /lib/x86_64-linux-gnu/ (0x00007fc42da78000) => /lib/x86_64-linux-gnu/ (0x00007fc42da69000) => /lib/x86_64-linux-gnu/ (0x00007fc42da58000) => /lib/x86_64-linux-gnu/ (0x00007fc42da3c000) => /lib/x86_64-linux-gnu/ (0x00007fc42da1f000) => /lib/x86_64-linux-gnu/ (0x00007fc42d9d8000) => /lib/x86_64-linux-gnu/ (0x00007fc42d802000) => /lib/x86_64-linux-gnu/ (0x00007fc42d7fb000) => /lib/x86_64-linux-gnu/ (0x00007fc42d7ef000) => /lib/x86_64-linux-gnu/ (0x00007fc42d75c000) => /lib/x86_64-linux-gnu/ (0x00007fc42d6b3000) => /lib/x86_64-linux-gnu/ (0x00007fc42d67b000) => /lib/x86_64-linux-gnu/ (0x00007fc42d662000) => /lib/x86_64-linux-gnu/ (0x00007fc42d52c000) => /lib/x86_64-linux-gnu/ (0x00007fc42d50a000) => /lib/x86_64-linux-gnu/ (0x00007fc42d388000) => /lib/x86_64-linux-gnu/ (0x00007fc42d370000) => /lib/x86_64-linux-gnu/ (0x00007fc42d336000) => /lib/x86_64-linux-gnu/ (0x00007fc42d2fe000) => /lib/x86_64-linux-gnu/ (0x00007fc42d27a000) => /lib/x86_64-linux-gnu/ (0x00007fc42d250000) => /lib/x86_64-linux-gnu/ (0x00007fc42d23e000) => /lib/x86_64-linux-gnu/ (0x00007fc42d1ee000) => /lib/x86_64-linux-gnu/ (0x00007fc42d0c5000) => /lib/x86_64-linux-gnu/ (0x00007fc42d08a000) => /lib/x86_64-linux-gnu/ (0x00007fc42d07e000)

How to make that all dependiency is build in executable?

Try building with the musl target.

Good to know, thank you very much.

It doesn't work :frowning: shared system libraries are still required :confused:

cargo build --target x86_64-unknown-linux-musl --release


   Compiling r2d2-diesel v1.0.0
   Compiling core v0.1.0 (/home/marcin/projects/rust/rust-nms/core)
   Compiling api v0.1.0 (/home/marcin/projects/rust/rust-nms/api)
   Compiling web v0.1.0 (/home/marcin/projects/rust/rust-nms/web)
   Compiling nms v0.1.0 (/home/marcin/projects/rust/rust-nms/nms)
    Finished release [optimized] target(s) in 1m 41s

$ ldd target/x86_64-unknown-linux-musl/release/nms (0x00007fffa35e5000) => /lib/x86_64-linux-gnu/ (0x00007f7df7321000) => /lib/x86_64-linux-gnu/ (0x00007f7df728f000) => /lib/x86_64-linux-gnu/ (0x00007f7df6fa3000) => /lib/x86_64-linux-gnu/ (0x00007f7df6f56000) => /lib/x86_64-linux-gnu/ (0x00007f7df6f00000) => /lib/x86_64-linux-gnu/ (0x00007f7df6edd000) => /lib/x86_64-linux-gnu/ (0x00007f7df6ce9000) => /lib/x86_64-linux-gnu/ (0x00007f7df6ce3000) => /lib/x86_64-linux-gnu/ (0x00007f7df6c06000) => /lib/x86_64-linux-gnu/ (0x00007f7df6bd5000) => /lib/x86_64-linux-gnu/ (0x00007f7df6bce000) => /lib/x86_64-linux-gnu/ (0x00007f7df6bbf000) => /lib/x86_64-linux-gnu/ (0x00007f7df6bac000) => /lib/x86_64-linux-gnu/ (0x00007f7df6b90000) => /lib/x86_64-linux-gnu/ (0x00007f7df6b73000) => /lib/x86_64-linux-gnu/ (0x00007f7df6b2e000) => /lib/x86_64-linux-gnu/ (0x00007f7df6958000)
        /lib64/ (0x00007f7df75e0000) => /lib/x86_64-linux-gnu/ (0x00007f7df6951000) => /lib/x86_64-linux-gnu/ (0x00007f7df6943000) => /lib/x86_64-linux-gnu/ (0x00007f7df68b0000) => /lib/x86_64-linux-gnu/ (0x00007f7df6809000) => /lib/x86_64-linux-gnu/ (0x00007f7df67d1000) => /lib/x86_64-linux-gnu/ (0x00007f7df67b8000) => /lib/x86_64-linux-gnu/ (0x00007f7df6680000) => /lib/x86_64-linux-gnu/ (0x00007f7df665e000) => /lib/x86_64-linux-gnu/ (0x00007f7df64dc000) => /lib/x86_64-linux-gnu/ (0x00007f7df64c6000) => /lib/x86_64-linux-gnu/ (0x00007f7df648c000) => /lib/x86_64-linux-gnu/ (0x00007f7df6454000) => /lib/x86_64-linux-gnu/ (0x00007f7df63ce000) => /lib/x86_64-linux-gnu/ (0x00007f7df63a4000) => /lib/x86_64-linux-gnu/ (0x00007f7df6392000) => /lib/x86_64-linux-gnu/ (0x00007f7df6344000) => /lib/x86_64-linux-gnu/ (0x00007f7df621b000) => /lib/x86_64-linux-gnu/ (0x00007f7df61e0000) => /lib/x86_64-linux-gnu/ (0x00007f7df61d2000) => /lib/x86_64-linux-gnu/ (0x00007f7df6083000)

You will need to look up which dependencies those shared libraries come from and find the corresponding -sys crate. You can then check if the -sys crate has some special option to turn on static compilation for that -sys crate, and use that option if so.

If the -sys crate does not provide the option, the only way is to modify that -sys crate to add support for static linking.

You might also want to open an issue on those -sys crates and ask them to default to static linking when using the musl target, as recommended by this article.

In my case, diesel provides dependieces with libpq
diesel = { version="1.4.5", features = ["postgres"] }

and when I try to run executable build with MUSL I get segmentation fault

Initializing router
Starting server
🔧 Configured for production.
    => address:
    => port: 8000
    => log: critical
    => workers: 16
    => secret key: generated
    => limits: forms = 32KiB
    => keep-alive: 5s
    => read timeout: 5s
    => write timeout: 5s
    => tls: disabled
Warning: environment is 'production', but no `secret_key` is configured
Segmentation fault (core dumped)

Looking at the source for pq-sys which provides the postgres bindings, it seems like you might be able to build with the environment variable PQ_LIB_STATIC=true to get libpq to be linked statically. Not sure that thats what's causing the segfault but it's worth a shot.

Instead of statically linking everything, for an application with a large number of dynamically linked dependencies have you thought about putting it inside a Docker container?

Thank you, yes this should work but this causes another problems, because libpq needs another librararies which has to be set static also. With simple PQ_LIB_STATIC=true I get a lot of undefined references from linker.
I think that static linking with system's external libraries is not good idea.

Yes, I thought about this but docker isn't what I want to use. I'm going to create .deb and .rpm packages to provide it into production. So, in system packages I'll have to set dependieces and install it while installing my app.

I also don't know why :confused: Even with debug this only show "Segmentation fault" but in kern.log I'm getting this:

Mar 14 10:37:42 marcin-huawei kernel: [503224.325400] r2d2-worker-0[570760]: segfault at 0 ip 0000000000000000 sp 00007f4a0c3cb298 error 14
Mar 14 10:37:42 marcin-huawei kernel: [503224.325407] Code: Bad RIP value.
Mar 14 10:37:42 marcin-huawei kernel: [503224.325420] r2d2-worker-2[570762]: segfault at 0 ip 0000000000000000 sp 00007f4a0bfc2298 error 14
Mar 14 10:37:42 marcin-huawei kernel: [503224.325426] Code: Bad RIP value.

Thank you for interesting my issue.

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.