Re: global Variable in multiple files

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

 



Muthukumar Ratty wrote:
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?



It will. but will fail during loading!

I guess it will fail during loading, but only if the variable x is not defined anywhere in the rest of the kernel globally. Is my assumption correct?


Now I understand why the module will build successfully. The

static int x

declaration is specific to that file(file1.c) and the

extern int x

declaration in the other file(file2.c) will look for an x defined globally in the kernel at the time of loading.

This means the solution is still incorrect because if x is defined in the kernel, then the module will build but the references to x in file2.c will be to the x defined in the kernel.

And if x is not defined in the rest of the kernel, then the module will fail to load.

Can someone please explain this?









-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/


[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