I need to compile a module that is statically linked to lua library (liblua.a) and dynamically linked to dl library (libdl.so).
我需要编译一个静态链接到lua库(liblua.a)的模块,并动态链接到dl库(libdl.so)。
I've compiled the C source file (generic_loader.c) linking it to dl library:
我已经编译了将其链接到dl库的C源文件(generic_loader.c):
$ gcc -g generic_loader.c -shared -fpic -ldl -o _loader.o
No errors were shown as I can see the linked libraries and symbol resolution:
没有出现错误,我可以看到链接库和符号解析:
$ ldd _loader.o
_loader.o:
linux-vdso.so.1 => (0x00007fff231fe000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7397949000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7397582000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7397d6e000)
$ nm _loader.o
_loader.o:
0000000000201078 B __bss_start
0000000000201078 b completed.6972
w __cxa_finalize@@GLIBC_2.2.5
00000000000008d0 t deregister_tm_clones
U dlerror@@GLIBC_2.2.5
U dlopen@@GLIBC_2.2.5
U dlsym@@GLIBC_2.2.5
0000000000000940 t __do_global_dtors_aux
0000000000200df0 t __do_global_dtors_aux_fini_array_entry
0000000000201070 d __dso_handle
0000000000200e00 d _DYNAMIC
0000000000201078 D _edata
0000000000201080 B _end
0000000000000aec T _fini
0000000000000980 t frame_dummy
0000000000200de8 t __frame_dummy_init_array_entry
0000000000000ba8 r __FRAME_END__
0000000000201000 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
00000000000007e8 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000200df8 d __JCR_END__
0000000000200df8 d __JCR_LIST__
w _Jv_RegisterClasses
00000000000009b5 T load_as_global
0000000000000aab T luaopen_genericloader
U lua_pushboolean
U lua_pushcclosure
U lua_pushnil
U lua_pushstring
U lua_setfield
U lua_tolstring
0000000000000900 t register_tm_clones
0000000000201078 d __TMC_END__
The unresolved symbols belongs to lua library and should be handled in the next step, so I guess there is no problem with that binary.
未解决的符号属于lua库,应该在下一步中处理,所以我想这个二进制代码没有问题。
So I compiled the resulting binary _loader.so in order to statically link it to lib lua:
因此,我编译了生成的二进制_loader。因此,为了静态地链接到lib lua:
$ gcc -g -shared -fpic _loader.o /usr/local/lib/liblua.a -o genericloader.so
Again, no errors where shown. But when I list the symbols, all dl and lua symbols are missing, as well as load_as_global and luaopen_genericloader, both functions defined in generic_loader.c:
同样,没有显示错误。但是当我列出这些符号时,所有dl和lua符号都丢失了,还有load_as_global和luaopen_genericloader,这两个函数都是在generic_loader.c中定义的。
$ldd genericloader.so
genericloader.so:
linux-vdso.so.1 => (0x00007fff7cdfe000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f360ad0d000)
/lib64/ld-linux-x86-64.so.2 (0x00007f360b2f4000)
$ nm genericloader.so
genericloader.so:
0000000000201030 B __bss_start
0000000000201030 b completed.6972
w __cxa_finalize@@GLIBC_2.2.5
0000000000000530 t deregister_tm_clones
00000000000005a0 t __do_global_dtors_aux
0000000000200e08 t __do_global_dtors_aux_fini_array_entry
0000000000201028 d __dso_handle
0000000000200e18 d _DYNAMIC
0000000000201030 D _edata
0000000000201038 B _end
0000000000000618 T _fini
00000000000005e0 t frame_dummy
0000000000200e00 t __frame_dummy_init_array_entry
0000000000000628 r __FRAME_END__
0000000000201000 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
00000000000004e0 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000200e10 d __JCR_END__
0000000000200e10 d __JCR_LIST__
w _Jv_RegisterClasses
0000000000000560 t register_tm_clones
0000000000201030 d __TMC_END__
Am I missing something, a step in compilation or perhaps an option to gcc?
我是否遗漏了什么,是编译的一步,还是gcc的一个选项?
Thanks in advance.
提前谢谢。
1 个解决方案
#1
0
To mix dynamic and static linking you may need to use -Wl,-Bstatic
and -Wl,-Bdynamic
options as described in this SO answer.
为了混合动态和静态链接,您可能需要使用-Wl,-Bstatic和-Wl,-Bdynamic选项。
#1
0
To mix dynamic and static linking you may need to use -Wl,-Bstatic
and -Wl,-Bdynamic
options as described in this SO answer.
为了混合动态和静态链接,您可能需要使用-Wl,-Bstatic和-Wl,-Bdynamic选项。