Search code examples
dllddub

Is it's possible to link all libs statically in one exe?


Now my project in D have a lot of different libs. Is it's possible to link them statically and make single exe (the size is not problem).

Which command I should pass to DMD or is there any way to specify it's in DUB config?


Solution

  • As ratchet freak said, you will need to compile the dependencies as static libs themselves. Once you have the dependencies as static libraries, you can then list them in the libs entry of dub.json just as you would dynamic libraries.

    It may not be a shining example, but here is a dub.json I use to manage a project that I want to create both static and dynamic builds of. I use the configurations section to separate the static and dynamically linked builds. When building, I use the --config= flag to choose between the static and dynamic build.

    If you build static libs of the depenencies yourself and do not want to put them on your system's library search path, you can use the -L option in lflags to specify where the static libs are.

    Note that compiling your immediate dependencies statically does not necessarily mean your project will have no dynamic dependencies -- it may still link dynamically to the dependencies of your dependencies (unless you compile those statically too). For example, the project I linked above is statically linked to allegro and its modules, but is still dynamically linked to dependencies of allegro like libogg and libpng (because I did not compile static versions of those).