"Undefined reference to dlopen" when statically linking with gcc

4.1k views Asked by At

After reading up lots of gcc docomentation and similar questions my problem remains.
Im trying to statically link my libluajit.a into my project but no matter what combination of commands I try, one or another error pops up. Ive successfully compiled my project with dynamic linking though.
Right now Im out of ideas so heres what i got right now:

gcc_options = -std=c++11 -static -pthread

src_dir = LuaHost
src_files =  $(src_dir)/*.cpp
src_files += $(src_dir)/*.h
src_files += $(src_dir)/LuaSrc/*.h
src_files += $(src_dir)/LuaSrc/*.hpp

lib_cmd = -Wl,--no-as-needed -ldl -L./$(src_dir)/LuaSrc/ -lluajit

#compile everything and output an executeable
all:
    g++ $(gcc_options) $(src_files) $(lib_cmd) -o LuaJITSandbox.o

And heres some of the errors:

./LuaHost/LuaSrc/libluajit.a(lj_clib.o): In function `lj_clib_index':
lj_clib.c:(.text+0x1c0): undefined reference to `dlsym'
./LuaHost/LuaSrc/libluajit.a(lj_clib.o): In function `lj_clib_load':
lj_clib.c:(.text+0x2c8): undefined reference to `dlopen'
lj_clib.c:(.text+0x350): undefined reference to `dlerror'
lj_clib.c:(.text+0x424): undefined reference to `dlopen'

The libluajit.a has been compiled on the same machine, a RaspberryPi.

1

There are 1 answers

0
Florian Zwoch On

I think -static is not what you are after. -static will build a static application and does not mean link this static library to the application.

There are a few options here. When you link with -lluajit you could remove the the dynamic .so version of the library. gcc will default to dynamic linking, but will fallback to static linking when the dynamic library is not available or not found.

Instead of -lluajit you could just point to the static library file directly - treating it as an object input file: /usr/lib/libluajit.a.

I think the recommend way is to tell the linker how to link you library. Try using -Wl,-Bstatic -lluajit. You can switch between Bstaticand Bdynamic right in front of the library name in case you link to multiple libraries and want to link them differently.