Thread overview
How may I tell dub where to find a C library for linking?
Dec 10, 2018
Pablo De Nápoli
Dec 10, 2018
evilrat
Dec 10, 2018
Pab De Nápoli
Dec 10, 2018
Mike Parker
December 10, 2018
Hi,

I was playing with the example in

https://github.com/MoritzMaxeiner/llvm-d/tree/master/examples/fibonacci

When I try to build it using dub, the linker cannot find the LLVM library

$ dub build
Performing "debug" build using /usr/bin/dmd for x86_64.
llvm-d 2.4.1: target for configuration "native-target" is up to date.
fibonacci ~master: building configuration "link-single"...
Linking...
/usr/bin/ld: cannot find -lLLVM
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
/usr/bin/dmd failed with exit code 1.

On my system (Debian GNU/Linux 9, 64 bits) the library is in the directory /usr/lib/llvm-6.0/lib/

$ ls -l /usr/lib/llvm-6.0/lib/libLLVM.so
lrwxrwxrwx 1 root root 14 oct 24 19:44 /usr/lib/llvm-6.0/lib/libLLVM.so -> libLLVM-6.0.so

But how am I supposed to tell dub this, so it passes this information to the linker??

I guess I should set "libs" in dubs.json

Off course I have tried to read the dub documentation (like https://code.dlang.org/package-format?lang=json) but it was not clear enough to me. Please note that this library cannot be find by pkgconfig.

Many thanks!
Pablo
December 10, 2018
On Monday, 10 December 2018 at 02:59:21 UTC, Pablo De Nápoli wrote:
>
> On my system (Debian GNU/Linux 9, 64 bits) the library is in the directory /usr/lib/llvm-6.0/lib/
>
> $ ls -l /usr/lib/llvm-6.0/lib/libLLVM.so
> lrwxrwxrwx 1 root root 14 oct 24 19:44 /usr/lib/llvm-6.0/lib/libLLVM.so -> libLLVM-6.0.so
>
> But how am I supposed to tell dub this, so it passes this information to the linker??
>
> I guess I should set "libs" in dubs.json
>

IIRC your guess is correct. "libs" is passed directly to linker and is the only way to do it, so you just do smth like this

   "libs" : ["-L/usr/lib/llvm-6.0/lib/", "libLLVM-6.0"]

note that this -L will be extended for linker as -L-L...
(also note that you can do platform specific way with "libs-windows", "libs-posix"? to pick proper paths per platform)

Though I'm not a linux pro and don't remember if linking to .so is same as linking import library, if this is not the case you just need to set RPATH or whatever it is so it can find dynamic lib.
December 10, 2018
On Monday, 10 December 2018 at 05:24:16 UTC, evilrat wrote:
> On Monday, 10 December 2018 at 02:59:21 UTC, Pablo De Nápoli wrote:
>>
>> On my system (Debian GNU/Linux 9, 64 bits) the library is in the directory /usr/lib/llvm-6.0/lib/
>>
>> $ ls -l /usr/lib/llvm-6.0/lib/libLLVM.so
>> lrwxrwxrwx 1 root root 14 oct 24 19:44 /usr/lib/llvm-6.0/lib/libLLVM.so -> libLLVM-6.0.so
>>
>> But how am I supposed to tell dub this, so it passes this information to the linker??
>>
>> I guess I should set "libs" in dubs.json
>>
>
> IIRC your guess is correct. "libs" is passed directly to linker and is the only way to do it, so you just do smth like this
>
>    "libs" : ["-L/usr/lib/llvm-6.0/lib/", "libLLVM-6.0"]
>
> note that this -L will be extended for linker as -L-L...
> (also note that you can do platform specific way with "libs-windows", "libs-posix"? to pick proper paths per platform)
>
> Though I'm not a linux pro and don't remember if linking to .so is same as linking import library, if this is not the case you just need to set RPATH or whatever it is so it can find dynamic lib.

Many thanks but that didn't work,as I got the message

$ dub build
Performing "debug" build using /usr/bin/dmd for x86_64.
llvm-d 2.4.1: target for configuration "native-target" is up to date.
fibonacci ~master: building configuration "link-single"...
Linking...
/usr/bin/ld: cannot find -l-L/usr/lib/llvm-6.0/lib/
/usr/bin/ld: cannot find -llibLLVM-6.0
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
/usr/bin/dmd failed with exit code 1.

The problem is that GNU ld expects the -L for specifying the search path, not -l

What actually did work was:

1) Setting the LD_LIBRARY_PATH environment variable with

export LD_LIBRARY_PATH=/usr/lib/llvm-6.0/lib/

and using

 "libs" : ["LLVM-6.0"]

in dub.json. (this is somewhat nasty, it would be nice to keep all the information together in dub.json)

Moreover, the information about the path for linking with LLVM can be obtained from the shell script llvconfig as

$llvm-config-6.0 --libs --ldflags
-L/usr/lib/llvm-6.0/lib
-lLLVM-6.0

It would be better not to hard-code this information in dub.json but I don't know if it possible to invoke a shell script with something like
$(llvm-config-6.0 --libs --ldflags) (as you would do in a Makefile)

This method used to be employed by many other libraries besides llvm (but many are migrating to pkgconfig).

Many thanks!)





December 10, 2018
On Monday, 10 December 2018 at 15:38:24 UTC, Pab De Nápoli wrote:


> 1) Setting the LD_LIBRARY_PATH environment variable with
>
> export LD_LIBRARY_PATH=/usr/lib/llvm-6.0/lib/
>
> and using
>
>  "libs" : ["LLVM-6.0"]
>
> in dub.json. (this is somewhat nasty, it would be nice to keep all the information together in dub.json)
>
> Moreover, the information about the path for linking with LLVM can be obtained from the shell script llvconfig as
>
> $llvm-config-6.0 --libs --ldflags
> -L/usr/lib/llvm-6.0/lib
> -lLLVM-6.0
>

If you do want to put it in the dub config, the -L option, and any linker options, can go in the "lflags" directive. "libs" is for libraries only.

"lflags": ["-L/usr/lib/llvm-6.0/lib"],
"libs": ["LLVM-6.0"]