|
-
April 5th, 2006, 09:58 AM
#1
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)
-
April 5th, 2006, 10:11 AM
#2
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
-
April 5th, 2006, 10:32 AM
#3
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
If there is no love sun won't shine
-
April 5th, 2006, 10:49 AM
#4
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."
-
April 5th, 2006, 11:24 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|