Step by step tutorials for using bindings in D

Inkrementator invalid at email.com
Mon Mar 27 00:06:36 UTC 2023


On Sunday, 26 March 2023 at 18:49:57 UTC, eXodiquas wrote:
> On Sunday, 26 March 2023 at 14:09:19 UTC, Inkrementator wrote:
> But you said static bindings are the main portion of bindings 
> we use. So I tried to get static Lua bindings going.

Static Binding != Static Linking. Like I said, the terminology is 
not noob friendly and funnily enough, you might have taken away 
from the sentence the opposite of what is true.
To statically bind, you have to do nothing in 99.9% of cases I 
have never encountered a library before that handles it like the 
one you posted and does offers dynamic binding.
But for static/ dynamic *linking*, it's the opposite. If you 
don't type in a full path, but just `"libs": ["lua"]`, the 
compiler will by default pick the shared library if both static 
and shared are available. In general I would say it doesn't 
really matter until you want to distribute your app, then you 
have to think about it.

So **TL;DR**: Don't bother with static linking. Just say `"libs": 
["lua"]` and don't worry about it. In the following I try to 
explain what actually happens in the background, but 
understanding it is not important in the beginning. The following 
is only for your interest.

>`"lflags": ["-L/usr/local/lib/liblua.a"]`

I'm surprised this worked, according to `man ld`, the -L flag 
takes a dir as input, not a full filepath. Can you please post 
your full dub config? I'm intrigued.

> This works perfectly fine. I can also use `"libs": ["lua"]` so 
> I don't have to specify the complete path. But now I wonder, do 
> I have to specify all static bindings via linker commands or is 
> there a mechanism which allows me to specify a path where all 
> the libraries can be found? And my other question is, if I have 
> to specify all static libraries by name, how do I know the name 
> of the library? Giving the system path as `lflag` is easy, but 
> where does Lua get the `lua` name from which works in the 
> `"libs": ["lua"]` string?

These questions are related. `"libs": ["lua"]` will get 
translated to the compiler option `\<compiler> -llua`, who will 
search the library search path for files named 
liblua.so.\<Version>, and if it doesn't exist, liblua.a . This is 
why we have to give full file path of static libraries.
You can check the path here: `ld --verbose|grep SEARCH` and can 
add custom paths via the environment variable LD_LIBRARY_PATH
If you want to give (temporary) priority to static library, you 
can do so via `ld -Bstatic lib1 -Bdynamic lib2 file.o` or if you 
use a compiler, you have to pass the linkflag, so it's `gdc 
-Wl,-Bstatic -llib1 file.d`.

See: 
https://stackoverflow.com/questions/6578484/telling-gcc-directly-to-link-a-library-statically (gcc is a C compiler, but many concepts will map to D compilers, and almost all to [gdc](https://wiki.dlang.org/GDC) )

Sidenote: If you use this, you have to always make sure you have 
an `-Bdynamic` or `-Wl,-Bdynamic` at the end, as libc should 
always be linked dynamically.

Problem is, I don't think dub officially supports this. You could 
try:
`"lflags": ["-Bstatic", "-llua", "-Bdynamic"]`

But this would be kind of a hack, since now lua shouldn't be 
listed in the `"libs":"` section anymore. This is an artifact of 
the fact that not many people bother with static linking.
As you see, it's kind of a mess. To make matters worse, depending 
on the compiler and linker, these options might look different. 
gcc, ldc and dmd all use some kind of different options, though 
some stay the same: `-llua` will work for every compiler. But I 
suppose this is one of the reasons why not inserting hacks into 
dub like above makes sense, since then it can abstract over 
different compilers for you.

> Thanks for answering my noob questions. I've never dealt with 
> bindings and compiler flags in the languages I come from. :P

My pleasure. Answering these has been (un)surprisingly helpful in 
strengthening my own understanding.

____

PS: To really understand what is happening, you might want to try 
manually compiling a hello world program that depends on a 
library instead of using dub. Some pointers:
`dub build -v` will print out the compiler and linkflags used.

`pkg-config --libs --cflags lua` would generate compiler options 
for you. Use it like
`dmd $(pkg-config --libs --cflags lua) program.d`

If you decide to try this, I can walk you through it. But 
remember that it's not that important this is probably all a bit 
much.


More information about the Digitalmars-d-learn mailing list