Linux Linker

Not so experienced with linking in linux.

I've installed the lua baseline with
1
2
3
4
$ curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz
$ tar zxf lua-5.2.3.tar.gz
$ cd lua-5.2.3
$ sudo make install


It has installed liblua.a to /usr/local/lib/liblua.a

I try to link my library now and get:
1
2
3
4
5
6
7
8
9
10
11
$ make
g++ -o bin/luaTest int/luaWrap.o int/luaTest.o -L/usr/local/lib -llua
/usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc':
loadlib.c:(.text+0x7d2): undefined reference to `dlsym'
loadlib.c:(.text+0x819): undefined reference to `dlerror'
loadlib.c:(.text+0x846): undefined reference to `dlopen'
loadlib.c:(.text+0x8bc): undefined reference to `dlerror'
/usr/local/lib/liblua.a(loadlib.o): In function `gctm':
loadlib.c:(.text+0xa50): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
make: *** [bin/luaTest] Error 1 


How would I debug this? The library built fine, but now when I link to it and build I get errors inside of the 3rd party lib. When looking in the 3rd party source I notice this comment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#if defined(LUA_USE_DLOPEN)
/*
** {========================================================================
** This is an implementation of loadlib based on the dlfcn interface.
** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
** NetBSD, AIX 4.2, HPUX 11, and  probably most other Unix flavors, at least
** as an emulation layer on top of native functions.
** =========================================================================
*/

#include <dlfcn.h>

static void ll_unloadlib (void *lib) {
  dlclose(lib); // one of the purpetrators
}
// Other purpetrators below. 


Nevermind, found the answer... I needed to link to add -ldl to my linker line. Not sure how I would have figured that out without google!
Last edited on
Libraries use virtual inheritance when it comes to linking ;p
dlopen ... are in libdl.so
g++ -o bin/luaTest int/luaWrap.o int/luaTest.o -L/usr/local/lib -llua -ldl

LB
Libraries use virtual inheritance when it comes to linking
What?
When you statically link a library, you have to link its dependencies too. It's like virtual inheritance in C++.
Did you compiled lua with g++ or with gcc ? I think you compiled it with gcc since liblua is a C program.
I compiled it with g++. But I figured out the issue:
I needed to link to add -ldl to my linker line. Not sure how I would have figured that out without google!

Thanks
With linux you have pkg-config:

On my computer:
1
2
3
4
5
6
7
$ pkg-config --list-all | grep lua
lua5.2                   Lua - Lua language engine
lua5.1                   Lua - Lua language engine
lua5.1-c++               Lua - Lua language engine

$ pkg-config --libs lua5.1-c++
-llua5.1-c++


in a makefile:
lua_libs=$(shell pkg-config --libs lua5.1-c++)

It can help you with tricky things too:
1
2
3
4
5
6
7
8
$ pkg-config --libs sfml-graphics
-lsfml-graphics -lsfml-window -lsfml-system // the correct order

$ pkg-config --libs sfml-window sfml-graphics
-lsfml-graphics -lsfml-window -lsfml-system // no duplication

$ pkg-config --cflags sdl2
-D_REENTRANT -I/usr/include/SDL2


Hopefully you have something when you call pkg-config --list-all | grep lua and when you use it, it puts that -ldl in there too.
Last edited on
Registered users can post here. Sign in or register to post.