c++ - 通过 dlsym 访问 C 中的阴影全局变量不起作用

标签 c++ c linux

int myvar=100;

main()
{
  pid_t (*f) (void );
  int *ip;
  int myvar=10;

  f = (pid_t(*) (void)) dlsym(RTLD_NEXT, "getpid");
  ip = (int *) dlsym(RTLD_NEXT, "errno");
  if (ip) 
    printf("%d \r\n", *ip);
  ip = (int *) dlsym(RTLD_NEXT, "myvar");
  if (ip) 
    printf("%d \r\n", *ip);  //not work where is i am wrong
}

最佳答案

getpiderrno 符号由 libc 导出。 myvvar 符号在您的可执行文件中,可能未导出。

objdump -T 会帮你检查符号是否导出。

$ objdump -T /lib/libc.so.6 | grep -w getpid
00c9c570 g    DF .text  00000036  GLIBC_2.0   getpid

关于c++ - 通过 dlsym 访问 C 中的阴影全局变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4612904/

相关文章:

c - 开源 C 嵌入式 Web 服务器,支持基于 SOAP/JSON-RPC 的 Web 服务并与 ARM 处理器兼容

java - 在编写时也使 java 中新创建的文件不可读

c++ - 为什么在我的 Apache 2.2 模块中调用 ap_hook_type_checker 函数时 request_rec::filename NULL?

linux - 在 X.Org 中控制对显卡的访问

c++ - 区分函数重载和函数覆盖

c++ - QDirIterator迭代顺序

c++将图像发送到打印机,(PRINT)

c - 我必须为 NULL 指针进行 malloc 吗?我什么时候需要为指针进行 malloc?

c++ - std::accumulate 有引用吗?

c - 更新 Arduino 中的结构