8

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!

1 Answer 1

10

You need to compile your main application with -rdynamic flag (eg: gcc -g -rdynamic -o main main.c, and to declare the global variable in your dynamic library with extern.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you peoro, I had to pass the -rdynamic flag to the linker. Now it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.