CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    The use of extern

    My understanding of using extern is;
    1) It used as a declaration without being defined;
    2) Any new declarion with definition of the same name will be treated as being the same as the extern declaration. For example.

    In Module1.c
    Code:
    extern HANDLE hMutex;
    
    dwWaitResult = WaitForSingleObject(hMutex, 20);
    now, this is where the confusion lies...

    in Module2.C

    Code:
    extern HANDLE hMutex;  // does this take the value from within Module1.c?
    
    // so that I can then do the following...
    ReleaseMutex(hMutex);
    Regards
    I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)

  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: The use of extern

    There isn't an hMutex in Module1.c

    The linker will try to find an instance of hMutex and will use it for both Module1.c and Module2.C

  3. #3
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: The use of extern

    Both of your declarations tells that the hMutex variable is declared but defined somewhere else. If the linker doesn't find the definition of hMutex somewhere else then u will have a link error.

    So somewhere u should have:

    Moudule3.cpp
    Code:
    Handle hMutex;
    If there is no love sun won't shine

  4. #4
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: The use of extern

    You'll have 'unresolved external symbol' linker error. When you declare variable with specifier extern, you effectively say "there is a variable there somewhere, I want to access it using this symbol". If linker can't find that variable, it can't link usage of symbol with actual variable, and so program code is incomplete.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  5. #5
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    Re: The use of extern

    Sorry folks, the hMutex is indeed defined and should've been defined in Module1, but I guess I must've omitted that out by accident. But the explanition given by NMTop40 has sufficed.

    Thanx guys.
    I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)

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