how to import .lib library

jfondren jfondren at minimaltype.com
Sun Aug 15 10:40:36 UTC 2021


On Sunday, 15 August 2021 at 10:19:33 UTC, Mike Parker wrote:
> On Sunday, 15 August 2021 at 10:12:17 UTC, Timofeyka wrote:
>
>> Thank you for your reply!
>> I wanted to link to my project another project without source 
>> code.
>
> Yeah, that's not possible. You either need the source or a set 
> of D interface files that declares all the symbols you need.

Meaning, it is possible. On Windows where I assume these .lib 
files are:

```
PS C:\Users\jfond> cat math.d
extern(C) int twice(int n) { return n * 2; }

PS C:\Users\jfond> cat mathuser.d
extern (C) int twice(int n);

void main() {
     import std.stdio : writeln;

     writeln(twice(21));
}

PS C:\Users\jfond> ldc2 -lib math.d
PS C:\Users\jfond> ldc2 mathuser.d math.lib
PS C:\Users\jfond> ./mathuser
42
```

math.lib is written in D but it could've been written just as 
well in C or C++ or anything, as long as it's targeting the C ABI 
in whatever language.

When mathuser.d is compiled, D does not need the source for 
math.lib. That one extern(C) function without a body is 
sufficient to, again targeting the C ABI, say "I am expecting to 
be linked with a function like this", and math.lib supplies that 
function at link time.

D is identical to pretty much every other native-compiled 
language in this respect.

The question you probably want to be asking is, "given a specific 
library from this vendor, what's the most *convenient* way to 
link D against it", or "how should I tell dub to link this D 
application with a .lib file in a parent directory", etc.



More information about the Digitalmars-d-learn mailing list