Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Globals and...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 14 Topic 26067 of 26972
Post > Topic >>

Re: Globals and Ex****ts

by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= <toe@[EMAIL PROTECTED] > May 5, 2008 at 09:55 AM

On May 5, 10:53=A0am, "Bartc" <b...@[EMAIL PROTECTED]
> wrote:
> I noticed that in C, functions in any module are automatically ex****ted.
S=
o
> that it's not possible to use the same function name in two modules (ie.
> source files).


You can't have two functions with the same name, correct. (I changed
your wording because it could have meant that you can't call a
function from another translation unit).


> Now that I know that, I get work around it; but is there a way to avoid
th=
e
> problem (short of lots of renaming)?
>
> More seriously, variables declared at file scope also seem to be
> automatically ex****ted.


The linkage of a function or object can be either "extern" or
"static". You are right in thinking that the default is "extern", but
beware of the exception to the rule: if the object is const, then the
default becomes "static". I.e. the following two global object
definitions are the same:

    int const i =3D 5;

    static int const i =3D 5;


> But in this case, the compiler/linker doesn't warn
> me that the same name is being used in two or more modules. (Is this
what
> the fuss is about with 'global variables'?)
>
> Is there any way I can fix this? (Like some keyword that will render a
> variable local to a module.)


Instead of having:

    int GetNumber(void) { return 5; }

you have:

    static int GetNumber(void) { return 5; }

The difference this makes is that when the compiler produces an object
file for the translation unit in question, it doesn't list "GetNumber"
in the object file's list of functions. Therefore, when the linker
links different object files together, it never sees the "GetNumber".
The net effect of this is:

1) You can have a function with the same name in a different
translation unit.
2) You cannot call the function from a different translation unit.

There is another kind of visiblity problem you might encounter. Let's
say you're writing a program that deals with Ethernet and also with
USB. For this, you might be using two different libraries, one for the
Ethernet and one for the USB. The Ethernet library might have a
function called "CheckConnection", and the USB library might have a
function by exactly the same name.

Since we want both these functions to be ex****ted from their
respective object files, we have a problem.

In C++, the introduced the concept of "namespaces". Basically you'd
have:

    USB::CheckConnection();
    Ethernet::CheckConnection();

In C though, I think the best you can do is:

    USB_CheckConnection();
    Ethernet_CheckConnection();

That is to say, you'd have to actually change the function's name,
perhaps by prepending the library name to it.
 




 14 Posts in Topic:
Globals and Exports
"Bartc" <bc@  2008-05-05 09:53:12 
Re: Globals and Exports
Antoninus Twink <nospa  2008-05-05 12:12:43 
Re: Globals and Exports
"Bartc" <bc@  2008-05-05 11:00:02 
Re: Globals and Exports
Flash Gordon <spam@[EM  2008-05-05 12:12:28 
Re: Globals and Exports
Flash Gordon <spam@[EM  2008-05-05 11:40:48 
Re: Globals and Exports
Mark McIntyre <markmci  2008-05-05 12:18:00 
Re: Globals and Exports
Antoninus Twink <nospa  2008-05-05 15:00:02 
Re: Globals and Exports
Flash Gordon <spam@[EM  2008-05-05 14:16:25 
Re: Globals and Exports
"Malcolm McLean"  2008-05-05 21:40:52 
Re: Globals and Exports
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-05 09:55:27 
Re: Globals and Exports
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-05 10:02:48 
Re: Globals and Exports
Martin Ambuhl <mambuhl  2008-05-05 15:06:55 
Re: Globals and Exports
John Bode <john_bode@[  2008-05-05 16:07:30 
Re: Globals and Exports
Nick Keighley <nick_ke  2008-05-06 03:17:52 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 16:08:13 CDT 2008.