Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

lunanera

trying to grasp extern

This topic is 5276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to grasp the workings of the extern keyword but I am having some problems... From what I understand, the extern keyword is put before a variable or function declaration to tell the compiler that memory for this variable is allocated elsewhere, usually in some other file. What I don''t understand is how does the compiler know in which file this variable is properly declared and allocated? for example in the project I am working on I have encountered this in a header file which does not #include <time.h> :
/*  functions */
extern time_t mktime __ARGS((const struct tm * ));
extern struct tm *gmtime __ARGS((const time_t *, struct tm *));
 
how does the compiler know where to look for the bodies of these functions? or did I just get the whole extern business wrong?

Share this post


Link to post
Share on other sites
Advertisement
The compiler (or the linker, I''m not sure which one) knows where everything is.

So the use of extern is not to tell the compiler where the symbol is defined, but rather to tell it that is accessible within this scope.

As to the ''how'' that''s a compiler issue that need not concern the programmer (unless the programmer was writing a compiler themself)

Peace Out!

Share this post


Link to post
Share on other sites
So I guess that it''s up to the makefile to tell the linker which modules depend on which...

thanks

~nz <--- don''t destruct yourself! hihihihi crap joke

Share this post


Link to post
Share on other sites
No, the makefile doesn''t need to tell the linker which object files depend on which. It just tells the linker which object files to link together. Each object file then has a list of what symbols they export and which symbols they need to import. The linker then runs through each object file, making a list of all the symbols exported (then flagging an error if multiple object files export the same symbol) and then checking all the imported symbols to make sure that all the symbols being imported are actually exported by other object files in the current link step. (I''m skipping some steps, but thats the basic gist of it.)

Share this post


Link to post
Share on other sites
The compiler only ever sees 1 file at a time. When it sees an extern, you''re telling it "this variable is declared elsewhere, assume it works" and the compiler will simply pretend it is as it was declared and move on.

The linker has access to all of the object files that are going to be combined into your final binary. When a source file goes through the compiler, the extern is replaced with a placeholder. The linker takes this placeholder and replaces it with the actual variable information. If it can''t find the variable in any of the object files, it''ll give you a linker error.

Share this post


Link to post
Share on other sites
quote:

The compiler only ever sees 1 file at a time. When it sees an extern, you''re telling it "this variable is declared elsewhere, assume it works" and the compiler will simply pretend it is as it was declared and move on.

The linker has access to all of the object files that are going to be combined into your final binary. When a source file goes through the compiler, the extern is replaced with a placeholder. The linker takes this placeholder and replaces it with the actual variable information. If it can''t find the variable in any of the object files, it''ll give you a linker error.


That''s the best explanation of extern I''ve ever seen. Back on topic: what he said ^.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!