How to link to libdl under linux

Mike Parker aldacron at gmail.com
Mon Dec 16 04:27:24 PST 2013


On 12/16/2013 7:09 PM, MrSmith wrote:
>> You're still not linking ld properly. It would help tremendously if
>> you could show the command line you're using. Otherwise all people can
>> do is make guesses about what *might* be wrong.
>
> Oh, right.
> Here is first with "/usr/lib32/libdl.a" -L-ldl
>
> andrey at andress-ubuntu:~/anchovy$ dmd -debug -gc -m32
> "examples/fpshelper.d" "examples/main.d" -I"import" -I"deps/dlib"
> -I"deps/derelict-fi-master/source" -I"deps/derelict-sdl2-master/source"
> -I"deps/derelict-ft-master/source" -I"deps/derelict-gl3-master/source"
> -I"deps/derelict-glfw3-master/source"
> -I"deps/derelict-util-1.0.0/source" "lib/debug/libgui.a"
> "lib/debug/libgraphics.a" "lib/debug/libcore.a" "lib/debug/libutils.a"
> "deps/dlib/libdlib.a" "deps/derelict-fi-master/lib/libDerelictFI.a"
> "deps/derelict-ft-master/lib/libDerelictFT.a"
> "deps/derelict-gl3-master/lib/libDerelictGL3.a"
> "deps/derelict-glfw3-master/lib/libDerelictGLFW3.a"
> "deps/derelict-util-1.0.0/lib/libDerelictUtil.a" "/usr/lib32/libdl.a"
> -L-ldl -of"bin/guidemo"
>
> deps/derelict-fi-master/lib/libDerelictFI.a(sharedlib_416_4ee.o): In
> function `_D8derelict4util9sharedlib13LoadSharedLibFAyaZPv':
> /home/andrey/anchovy/deps/derelict-fi-master/../../../.dub/packages/derelict-util-1.0.0/source/derelict/util/sharedlib.d:60:
> warning: Using 'dlopen' in statically linked applications requires at
> runtime the shared libraries from the glibc version used for linking
> /usr/lib32/libdl.a(dlopen.o): In function `dlopen':
> (.text+0x1b): undefined reference to `__dlopen'
> /usr/lib32/libdl.a(dlclose.o): In function `dlclose':
> (.text+0x1): undefined reference to `__dlclose'
> /usr/lib32/libdl.a(dlsym.o): In function `dlsym':
> (.text+0x1b): undefined reference to `__dlsym'
> /usr/lib32/libdl.a(dlerror.o): In function `dlerror':
> (.text+0x1): undefined reference to `__dlerror'
> collect2: error: ld returned 1 exit status
> --- errorlevel 1
>
> Second with -L-ldl only
>
> andrey at andress-ubuntu:~/anchovy$ dmd -debug -gc -m32
> "examples/fpshelper.d" "examples/main.d" -I"import" -I"deps/dlib"
> -I"deps/derelict-fi-master/source" -I"deps/derelict-sdl2-master/source"
> -I"deps/derelict-ft-master/source" -I"deps/derelict-gl3-master/source"
> -I"deps/derelict-glfw3-master/source"
> -I"deps/derelict-util-1.0.0/source" "lib/debug/libgui.a"
> "lib/debug/libgraphics.a" "lib/debug/libcore.a" "lib/debug/libutils.a"
> "deps/dlib/libdlib.a" "deps/derelict-fi-master/lib/libDerelictFI.a"
> "deps/derelict-ft-master/lib/libDerelictFT.a"
> "deps/derelict-gl3-master/lib/libDerelictGL3.a"
> "deps/derelict-glfw3-master/lib/libDerelictGLFW3.a"
> "deps/derelict-util-1.0.0/lib/libDerelictUtil.a" -L-ldl -of"bin/guidemo"
>
> deps/derelict-fi-master/lib/libDerelictFI.a(sharedlib_416_4ee.o): In
> function `_D8derelict4util9sharedlib13LoadSharedLibFAyaZPv':
> /home/andrey/anchovy/deps/derelict-fi-master/../../../.dub/packages/derelict-util-1.0.0/source/derelict/util/sharedlib.d:60:
> undefined reference to `dlopen'
> deps/derelict-fi-master/lib/libDerelictFI.a(sharedlib_417_5d1.o): In
> function `_D8derelict4util9sharedlib15UnloadSharedLibFPvZv':
> /home/andrey/anchovy/deps/derelict-fi-master/../../../.dub/packages/derelict-util-1.0.0/source/derelict/util/sharedlib.d:64:
> undefined reference to `dlclose'
> deps/derelict-fi-master/lib/libDerelictFI.a(sharedlib_418_396.o): In
> function `_D8derelict4util9sharedlib9GetSymbolFPvAyaZPv':
> /home/andrey/anchovy/deps/derelict-fi-master/../../../.dub/packages/derelict-util-1.0.0/source/derelict/util/sharedlib.d:68:
> undefined reference to `dlsym'
> deps/derelict-fi-master/lib/libDerelictFI.a(sharedlib_419_463.o): In
> function `_D8derelict4util9sharedlib11GetErrorStrFZAya':
> /home/andrey/anchovy/deps/derelict-fi-master/../../../.dub/packages/derelict-util-1.0.0/source/derelict/util/sharedlib.d:72:
> undefined reference to `dlerror'
> collect2: error: ld returned 1 exit status
> --- errorlevel 1

For starters, remove "/usr/lib32/libdl.a". All you need is -L-ldl. The 
-L flag tells DMD that what follows needs to be passed to the linker. 
-ldl tells the linker to link with libdl. It will link with the shared 
version, which is really what you want. As it stands, you're telling DMD 
to link with both the static version of libdl (which you don't have 
installed, I think, according to the error in your original post) and 
the shared version at the same time. I don't know if that's the source 
of your current linker errors, but it's certainly potentially problematic.

Also, since you aren't using dub to manage your project, I suggest you 
copy all of the Derelict libraries to a common location. Then, you can 
remove all of the "*/libDerelict*.a" from the command line and do this 
instead:

-L-Lpath/to/libs -L-lDerelictFI -L-lDerelictGL3...

On the other hand, if you use dub to manage your project, all of this, 
including libdl, will be linked in for you automatically. Then your 
command line becomes:

dub build

during development and

dub build --build=release

when you're ready for the release version.


More information about the Digitalmars-d-learn mailing list