Adding libraries to an executable

Rikki Cattermole alphaglosined at gmail.com
Sat Sep 7 19:21:25 PDT 2013


On Saturday, 7 September 2013 at 13:39:49 UTC, Anton Alexeev 
wrote:
> On Saturday, 7 September 2013 at 10:13:39 UTC, Rikki Cattermole 
> wrote:
>> On Saturday, 7 September 2013 at 09:05:39 UTC, Rikki 
>> Cattermole wrote:
>>> On Friday, 6 September 2013 at 21:31:59 UTC, Anton Alexeev 
>>> wrote:
>>>> I've written a program which uses curl library. So on a PC 
>>>> without curl the program will not work until you install the 
>>>> library. Is there a way to put the curl library in the 
>>>> executable?
>>>
>>> You just gave me an idea to compile in shared libraries and 
>>> then to extract them on run.
>>> I'll let you know when I got some code.
>>> (Btw this is not a good idea but definitely will work for 
>>> license reasons, although this could be a rather gray zone. 
>>> Should be confirmed.).
>>
>> So I modified my Bin2D which allows compiling in resources[1].
>> Now it can produce an output file that when compiled in gives 
>> two new functions.
>> These functions essentially extract the files for you.
>> One of those is for temporary directory[2].
>>
>> Little bit off topic but it can be utilised for packaging up 
>> shared libraries into your executable and then extracting on 
>> run.
>>
>> E.g.
>> $ dub build
>> $ cd bin
>> $ ./Bin2D outputFile.d=outputModule lib1.so lib2.dll
>> Just an example usage. Note not on dub repository.
>>
>> Just another option for ya.
>>
>> [1] 
>> https://github.com/rikkimax/Bin2D/blob/master/source/Bin2D.d
>> [2] string[string] outputBin2D2FS()
>
> Thank you for taking time and writing this tool but I don't 
> really understand how to use it. Can you please explain it 
> little bit more? So I have my main.d file with source code and 
> I compile it with this command: "dmd -L-lphobos2 -L-lcurl 
> main.d". How will I get all the libraries into one executable 
> with Bin2D?

First I may have forgotten that phobos links with curl not loads 
at runtime.
So if your wanting to use shared libraries like this over static 
still.
You can basically compile normally.
Then $ Bin2D output.d=output main curl.so
Where main is the executable you built with dmd.
You will need to add a main function to output.d e.g.

import std.process;
void main() {
     string[string] files = outputBin2D2FS();
     execute(files["main"]);
}

Note that the usage of std.process is from memory.

Although I would highly recommend using static instead if you can.


More information about the Digitalmars-d-learn mailing list