Re: global Variable in multiple files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I use this method in several driver modules for
various PCI-based data acquisition and DSP devices
that my employer manufactures so I can vouch that the
linker will not complain. I use this method for both
variables and functions. (This does assume that you're
linking with the relocatable '-r' option.)

Defining an object as static doesn't mean "hide it
from other translation units" so much as it means
"don't make it visible to other translation units." 

The linking that is done between the various source
object files is essentially a merge operation that
makes one aggregate translation unit from all source
translation units.

For reference, here's the relevent portion of one of
my module's makefile:

#
all: $(TARGET).o

$(TARGET).o : p14_main.o p14_intr.o p14_dma.o
     $(LD) -r $^ -o $@
#

-Mike


--- Vishwas Raman <vishwas@eternal-systems.com> wrote:
> Hi all:
> 
> Pardon me if my question seems very illogical, but I
> guess the stupidest 
> question is the one that goes unasked.
> 
> If we do the following
> 
> static int x; // in file1.c
> 
> and
> 
> extern int x; // in file2.c
> 
> wouldn't the linker complain when the 2 object files
> are linked to 
> generate the .o for the module? Shouldn't specifying
> some variable as 
> static hide that variable from other source files.
> In the above case, I 
> agree that x will not be exported, but will the
> module even build 
> successfully?
> 
> -Vishwas.
> 
> 
> 
> Mike DeKoker wrote:
> 
> > Yes, you'll end up having a local symbol for the
> > static declaration and an undefined exported
> symbol
> > for the extern declaration. The local symbol is
> not
> > exported by definition. The undefined exported
> symbol
> > cannot be exported because there is no object
> (i.e.
> > variable or function) to which it is bound.
> > 
> > -Mike DeKoker
> > 
> > --- "Sponsel Carsten (ext)"
> > <carsten.sponsel@erln.gepas.de> wrote:
> > 
> >>But then I've two symbols when i view my module
> with
> >>nm...
> >>
> >>-----Original Message-----
> >>From: Mike DeKoker [mailto:mdekoker@yahoo.com]
> >>Sent: Freitag, 13. Februar 2004 18:20
> >>To: Sponsel Carsten (ext)
> >>Subject: Re: global Variable in multiple files
> >>
> >>
> >>Just define your variable as a module-level static
> >>and
> >>define an external reference to it in the other
> >>source
> >>files. This works for functions also.
> >>
> >>e.g.
> >>
> >>/* File1.c */
> >>static int x;
> >>
> >>/* File2.c */
> >>extern int x;
> >>
> >>This assumes that File1.o and File2.o are being
> >>relocatably linked into a single object file.
> (Since
> >>you're coding for a module they should be.)
> >>
> >>-Mike DeKoker
> >>
> >>--- "Sponsel Carsten (ext)"
> >><carsten.sponsel@erln.gepas.de> wrote:
> >>
> >>>Hi friends,
> >>>
> >>>it might be a simple question:
> >>>
> >>>What's the best way to get (to define) a global
> >>>variable which can be used in multiple files of a
> >>>kernel module in Linux Kernel 2.6? The variable
> >>>should not be exported as a symbol. In my special
> >>>problem I need an array of a struct which is
> >>
> >>useable
> >>
> >>>in all sourcefiles which form my kernel module.
> >>>
> >>>thanks,
> >>>Carsten
> >>>
> >>
> >>snip
> >>
> >>__________________________________
> >>Do you Yahoo!?
> >>Yahoo! Finance: Get your refund fast by filing
> >>online.
> >>http://taxes.yahoo.com/filing.html
> > 
> > 
> > 
> > __________________________________
> > Do you Yahoo!?
> > Yahoo! Finance: Get your refund fast by filing
> online.
> > http://taxes.yahoo.com/filing.html
> > 
> > --
> > Kernelnewbies: Help each other learn about the
> Linux kernel.
> > Archive:      
> http://mail.nl.linux.org/kernelnewbies/
> > FAQ:           http://kernelnewbies.org/faq/
> > 
> 
> 
> -- 
> --
> Vishwas Raman
> Software Engineer, Eternal Systems, Inc,
> 5290 Overpass Rd, Bldg D, Santa Barbara. CA 93111
> Email: vishwas@eternal-systems.com
> Tel:   (805) 696-9051 x246
> Fax:   (805) 696-9083
> URL:   http://www.eternal-systems.com/
> 
> --
> Kernelnewbies: Help each other learn about the Linux
> kernel.
> Archive:      
> http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
> 


__________________________________
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
http://taxes.yahoo.com/filing.html

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux