Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Smaug compiling using g++

Smaug compiling using g++

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #15 on Tue 11 Dec 2007 06:50 AM (UTC)
Message
Ah yes, good old extern, I didn't notice those were coming from a .h file.

OK... well, the thing is that whenever the compiler sees something of the form:

VAR_TYPE VAR_NAME;

it creates memory of size sizeof(VAR_TYPE) and all future occurrences of VAR_NAME will refer to that block of memory.

But sometimes you want to let several source files know that a given variable (i.e. named block of memory) exists. You could do something like VAR_TYPE VAR_NAME; in each source file, but that would be incorrect: that would create one block per file. That is the error you were getting, actually: the compiler was telling you that something was happening you didn't want.

The extern keyword tells the compiler not to allocate the memory, but to defer looking up that variable's memory address until the link phase. That way, all of your source files contain references to the variable name, but not the location in code; during link, the linker will go in and fix all those references to point to the right place.

So, you still need somewhere to have VAR_TYPE VAR_NAME in order for the compiler to allocate the memory. But you only want that in one spot: everywhere else where you want to [i]refer[/i] to that variable you need to have the extern keyword in front.

The intuition behind the keyword is that you are declaring that a variable is external with respect to the file being compiled, that is, you are assuring the compiler that the variable has memory out there somewhere and that it should defer looking that up until it has the external object files to link against.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


64,803 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.