Build fully static library by the compiler?

Denis Feklushkin feklushkin.denis at gmail.com
Sun Aug 11 11:08:24 UTC 2024


On Sunday, 11 August 2024 at 10:18:56 UTC, kinke wrote:
> On Friday, 9 August 2024 at 02:34:03 UTC, Denis Feklushkin 
> wrote:
>> Are there simple way to make a static library that also 
>> includes necessary standard D libraries (i.e., phobos2 and 
>> druntime)?
>
> Nope, you'd have to specify the path to the libs explicitly in 
> the cmdline to merge them into the created static lib/archive, 
> e.g., `ldc2 -lib bla.d $(dirname $(dirname $(which 
> ldc2)))/lib/lib{druntime,phobos2}-ldc.a`.

Even if I use my custom ldc2.conf? I can specify necessary 
options in it

>> Compiler already knows (?) paths to default static libs 
>> because it have --static option which produces static 
>> executable with all necessary libs inside. (Although I'm not 
>> sure that it works by this way)
>
> Not really; the `-defaultlib` libraries need to be in one of 
> the `lib-dirs` in ldc2.conf, it's the linker that resolves them.

That's exactly what I meant in this case

>> Point is that D can be not a main language of the project and 
>> it is unconvient to extract by somehow paths to phobos and 
>> druntime at last stages of project build.
>
> Would using the D compiler as linker driver be an option?

For whole project code (it is ESP IDF) it is impossible, as for 
me, because C linker is nearly "hardcoded" into it. I use ldc2 as 
compiler for my D code and would to use ldc2 for linking this 
code with D standard libraries.

> Similar to how people can avoid explicitly linking GNU's 
> libstdc++ / LLVM's libc++ by using `c++` instead of `cc` as 
> linker driver, thereby not having to know which implementation 
> of the C++ std library to choose for the particular platform.

Maybe in D case it will be right choice to respect --defaultlib= 
setting during static linking? So if anyone wants to link static 
library without stdlibs then just can leave it empty. In other 
cases druntime and phobos will be linked into static library?



More information about the Digitalmars-d-learn mailing list