Create D portable binary

Jacob Carlborg doob at me.com
Fri Dec 8 09:34:05 UTC 2017


On 2017-12-08 06:16, Fra Mecca wrote:
> Is there a way to compile a project and deploying it as a single 
> statically linked binary?
> 
> My main target would be something like a self contained jar (like .war 
> files), but something that is in the style of go binaries and portable 
> to another Linux distribution without any hassle would be enough.
> 
>  From what I understand the main problem in achieving this is the 
> dependency to curl (all the warnings related to gethostbyname during 
> linking phase) and glibc that makes it hard to static link.
> 
> What if musl is used as libc?

Currently the musl library cannot be used because the runtime relies on 
some GNU extension to standard libc.

> Right now it seems that the only viable option is to distribute object 
> files and make the end user link them

You can statically link a D binary using the -static flag and LDC, if 
you don't rely on any of the functions that the linker warns about, like 
gethostbyname. DMD cannot be used because of its TLS implementation.

For example, this project I've created is 100% statically linked on 
Linux. You can have a look in the Dub configuration [2].

If you need some resource files you can have a look at the Import 
Expression [3] as well.

[1] https://github.com/jacob-carlborg/remarkify
[2] https://github.com/jacob-carlborg/remarkify/blob/master/dub.sdl#L7
[3] https://dlang.org/spec/expression.html#import_expressions

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list