CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #1
    Join Date
    Feb 2008
    Posts
    53

    Smile The extern keyword

    Hello! I'm new to the forum and still in the process of learning C++. I'm learning C++ with "Teach Yourself C++ in 21 days" from Sams publishing. I'm on day 18 so i'm almost finished!

    But I've hit a snag. My book did an unusually poor job of explaining the idea of internal and external linkage. I've been reading different articles on the internet about the subject, but disagreement between the different articles, as well as with what my compiler does, has left me pretty confused.

    The extern keyword, according to one article, tells the compiler that the variable or function here, is declared in some other source file, and we'll be acccesing it in this source file. Whereas my book seems to say that declaring a variable external, tells the compiler that it can be accessed by other source files. Kind of a big difference there.
    Another article states, that if you were to create a header file, say file1.h which contained a variable declaration, such as 'int someInt;' and included this at the top of two source files, the linker wouldn't know if the two instances of that variable were supposed to be the same or different, and would complain of ambiguity.

    Well that appears to be incorrect, because when I declare 'int someInt' in a header file, and create two source files that use it, the linker does not complain of ambiguity, and experimenting has shown two instances of the variable exist.

    Can anyone explain to me whats going on?
    I think one of the biggest reasons I'm having trouble understanding this is I don't know how you could or would use more than one source file in the same program. Only one source file containing main() is allowed, and things in the other source file are generally hidden from the main source file. I don't see the point!
    Last edited by Mikau; February 17th, 2008 at 03:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured