[英]“Undefined reference to dlopen” when statically linking with gcc
After reading up lots of gcc docomentation and similar questions my problem remains. 阅读了很多gcc docomentation和类似的问题后,我的问题仍然存在。
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. 我试图将我的libluajit.a静态链接到我的项目中,但是无论我尝试使用哪种命令组合,都会弹出一个或另一个错误。 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. libluajit.a已在同一台计算机RaspberryPi上编译。
I think -static
is not what you are after. 我认为-static
不是您想要的。 -static
will build a static application
and does not mean link this static library to the application
. -static
将建立一个static application
并不意味着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. 与-lluajit
链接时,可以删除该库的动态.so
版本。 gcc will default to dynamic linking, but will fallback to static linking when the dynamic library is not available or not found. gcc将默认为动态链接,但是当动态库不可用或未找到时,它将退回到静态链接。
Instead of -lluajit
you could just point to the static library file directly - treating it as an object input file: /usr/lib/libluajit.a
. 代替-lluajit
您可以直接指向静态库文件-将其视为对象输入文件: /usr/lib/libluajit.a
。
I think the recommend way is to tell the linker how to link you library. 我认为推荐的方法是告诉链接程序如何链接您的库。 Try using -Wl,-Bstatic -lluajit
. 尝试使用-Wl,-Bstatic -lluajit
。 You can switch between Bstatic
and Bdynamic
right in front of the library name in case you link to multiple libraries and want to link them differently. 如果链接到多个库并希望以不同的方式链接它们,则可以在库名称前面的Bstatic
和Bdynamic
之间切换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.