Mat*_*son 3 linux dynamic-linking dynamic-loading
我在 linux 中有一个可执行文件 - exe
这个可执行文件中有一些函数,在整个代码中使用:
sendMsgdebugPrint然后我想动态加载一个.so为我的可执行文件提供额外功能的文件。
在这个共享库中,我包含了sendMsg和的头文件debugPrint。
我加载这个共享库dlopen()并使用dlsym().
但是,在dlopen()我用来RTLD_NOW在加载时解析所有符号。
它没有说明它找不到sendMsg符号。
这个符号必须在可执行文件中,因为sendMsg.c是在那里编译的。
但是,我的可执行文件被make进程剥离了。因此,dlopen找不到符号是有道理的。
我该如何解决这种情况?
exe及.so。这会增加代码大小:(exe以便可以找到符号.so知道符号在哪里exeman ld:
-E
--export-dynamic
--no-export-dynamic
When creating a dynamically linked executable, using the -E option or the --export-dynamic option causes the linker to add all symbols to the dynamic symbol table. The
dynamic symbol table is the set of symbols which are visible from dynamic objects at run time.
If you do not use either of these options (or use the --no-export-dynamic option to restore the default behavior), the dynamic symbol table will normally contain only those
symbols which are referenced by some dynamic object mentioned in the link.
If you use "dlopen" to load a dynamic object which needs to refer back to the symbols defined by the program, rather than some other dynamic object, then you will probably
need to use this option when linking the program itself.
You can also use the dynamic list to control what symbols should be added to the dynamic symbol table if the output format supports it. See the description of
--dynamic-list.
Note that this option is specific to ELF targeted ports. PE targets support a similar function to export all symbols from a DLL or EXE; see the description of
--export-all-symbols below.
Run Code Online (Sandbox Code Playgroud)
您还可以将-rdynamic选项传递给 gcc/g++(如注释中所述)。根据您设置 make 脚本的方式,这会很方便