It is currently Sat, 04 Feb 2023 12:55:15 GMT



 
Author Message
 Visibility of global variables in shared libraries
Hi,
    I've been looking up the various newsgroups in search of
information on global variables in ELF shared libraries on Linux but I
am getting conflicting answers to the following questions. Can someone
please help me out with definitive answers?

    * If a global variable is defined in a shared library, can the
*same instance* of the variable be accessed in a program that links
with the shared library ?
    * And what about vice versa ? i.e. if the global variable is in
the executable program that links with the library, then will
references to that variable in the shared library refer to the
instance in the executable, or to a separate copy?
    * Will the answers to the above be different depending on whether
the variable is initialized or uninitialized at the point of
definition ? And on whether the library is linked in on the gcc/ld
command line itself or subsequently loaded dynamically using
'dlopen()'

Finally, is there a canonical reference book/website where the
*practical* details of variable visiblity/scope/extent for libraries
are clearly explained ? I've seen the GCC and ELF HOWTOs but they're
not all that useful so I'd greatly appreciate any help.

Thanks
Nikhil



 Mon, 24 Oct 2005 18:01:52 GMT   
 Visibility of global variables in shared libraries

Yes (by default), on all UNICes.

However, you *can* change the default behaviour and build a shared
library such that a global defined it will not be visible outside. On
Linux, try 'info ld' and read the "Scripts" section.

[It is probably because of this, that you can't get a definitive
answer -- it depends on how things are built]

Yes, on *most* UNICes.

On Linux, to achieve that, you must use -Wl,--export-dynamic
when linking main executable.

On AIX, you must use 'deferred imports' and enable 'runtime linking'.

No.

No.

I don't know of any.

Cheers,
--
In order to understand recursion you must first understand recursion.



 Mon, 24 Oct 2005 23:05:24 GMT   
 Visibility of global variables in shared libraries
-------------------------------------------------------------^^^^^

It's an oxymoron to use an 'canonical reference' and Linux in the same
sentence.  Unless of course you include the source code.

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.



 Tue, 25 Oct 2005 04:33:57 GMT   
 Visibility of global variables in shared libraries
This helps a lot. Thanks much Paul.

Nikhil



 Tue, 25 Oct 2005 14:44:51 GMT   
 
   [ 4 post ] 

Similar Threads

1. Singleton global variable in a shared library

2. global variable with shared library

3. Global variables inside shared library on Solaris

4. Shared libraries and global variables

5. global variable in shared library?

6. global variable in shared library.

7. Problems with shared libraries and external global variables

8. Global data sharing in shared libraries

9. Shared variable between main() and loaded shared library - dlopen()

10. shared variables in shared libraries


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software