How do I build a statically linked executable with scala-native?
Asked Answered
S

2

9

I would like to write a program using Scala Native but it has to run on a linux host (centos 6.5) with a very old glibc. So I would like to deploy that program as a statically linked executable, e.g. as golang does.

How do I build a statically linked executable with scala-native? Is it possible? If yes, how do I do it? If no, why not?

Sumptuous answered 5/11, 2017 at 8:33 Comment(1)
Even I am looking for the answer to this question. If you have got the answer to this question kindly add it in the answer section. That will be helpful.Palate
C
0

The sbt option nativeLinkingOptions takes a Seq[String] of extra arguments which are passed verbatim to clang during linking.

Adding the following to the project build settings works for me in a quick test:

nativeLinkingOptions += "-static"
Counterstatement answered 3/6, 2021 at 6:14 Comment(0)
H
0

I'm not an expert on this topic, but from what I understand that's not something that can be done for all kinds of applications, it might be even impossible to do based on what are your dependency libraries;

For example you cannot statically link against glibc (it's not designed to be statically linkable and has dynamic features like nss that prevents it from being linked statically).

You can try another libc implementation like musl which is statically linkable, however you need to compile a lot of things from scratch and in the end, your output binary might have some weird behaviors, for example name resolution might not work and you need to use direct IP addresses, unless you use a resolver library too.

That's a rabbit hole you probably don't want to go to!

But, if you are also an experienced C/C++ developer, you can tweak with linker parameters as mentioned in the other answer; however you might need to link manually, as currently available sbt options does not allow you to change order of existing linking parameters, which is unfortunately important when linking statically!

Heidt answered 14/1, 2023 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.