tuban
tuban

Reputation: 5185

How to find the linking path for extern variable

Is there any way to find the file/shared_object from where linking is happenning for an extern variable used in current file/module.

for example: In a large application sofware in linux, I want to find the declaration of a particular variable that I have externed in my module ...

Thanks in advance.

Upvotes: 1

Views: 233

Answers (1)

I am not entirely sure to understand your question, but perhaps dladdr should be suitable for your needs. dladdr is a Gnu/Glibc extension. From its manual: The function dladdr() takes a function pointer and tries to resolve name and file where it is located. (and it very probably could be used with the pointer to a global variable).

However, I am puzzled by the phrasing of your question. The "declaration of a variable" has no sense inside executable ELF binaries or shared objects, because declaration is essentially a source code concept, not an object code one. And practically speaking, most declarations (of global variables) are inside some header file.

Be aware of C++ name mangling

If you have the source code of your application, you could use textual tools (like grep or etags) or even extend the GCC compiler thru plugins or MELT extensions to find such declarations.

Of course, you can also use dlsym to find the address of some symbol, given its name.

Upvotes: 2

Related Questions