简体   繁体   中英

Using a global variable in a shared library

I am writing an application in C which used a global variable (a logfile structure). In my application I am loading shared libraries dynamically at runtime and I want to use a global variable pointing at the the same logfile structure to do logging in the shared library.

This doesn't seem to be possible in the easy approach:

  • declaring the global variable as extern will not work because dlopen() sais that the global variable is an undefined symbol
  • defining the global variable again will work but the "new" variable will not be the same as the "original" one in the executable

Any hint how to fix this would be great.

Thank you!

您需要使用-rdynamic标志编译主应用程序(例如: gcc -g -rdynamic -o main main.c ,并使用extern在动态库中声明全局变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM