zoukankan      html  css  js  c++  java
  • 一个动态链接的问题,dlsym后符号调用主函数的符号报告无法找到

    先看看状况(小心头疼)

    client.c 编译得到 client;在 client 的 main 中用 dlopen( "./liba.so", RTLD_LAZY|RTLD_GLOBAL) 打开 liba.so,并调用 liba.so 中的 start 函数;在 liba.so 的 start 函数中通过 dlopen( 0, RTLD_GLOBAL|RTLD_LAZY ) 得到的句柄尝试调用 client  中的 startup_fcn 函数。使用 gcc client.c -o client -ldl 得到的 client 与 gcc -fPIC -shared liba.c -o liba.so 得到的 liba.so 的时候发现 liba.so 无法找到 client 中定义的 startup_fcn 函数。后经 pacman2k 大侠的提醒,发现了 ld 的参数 -E / --export-dynamic。使用 gcc -Wl,-E client.c -o client 重新编译 client 程序之后就可以成功调用了。

    关于 -E 参数:

           -E
           --export-dynamic
               When  creating  a  dynamically  linked  executable, 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 this option, 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.
    最后一段正说道这种『回调的情况』

    关于 RTLD_GLOBAL

    经测试,所有 dlopen 均取消 RTLD_GLOBAL 参数之后,仍然运行正常。RTLD_GLOBAL 参数的作用有待考察。

  • 相关阅读:
    VC++基于ICMP 遍历路由表
    在XP上同时运行IE6,IE7,IE8,IE9
    VC++实现ARP协议
    在Windows Sever 2008 Windows7上同时运行绿色IE10测试HTML5
    VC++实践IOCP编程
    VC++实现检测网络状态所有的TCP与UDP通信
    VC++实现UDP数据包发送
    VC++实现改变网关
    面试十大难题的样板回答
    WinAPI: GetLocalTime、SetLocalTime、SetSystemTime 获取与设置系统时间
  • 原文地址:https://www.cnblogs.com/dancheblog/p/4262528.html
Copyright © 2011-2022 走看看