-
December 6th, 2012, 11:17 PM
#1
keyword extern
Could someone please explain the C/C++ keyword ' extern ' , and it use , if possible please post some example.
-
December 7th, 2012, 01:06 AM
#2
Re: keyword extern
Search the Web for C keyword extern.
-
December 7th, 2012, 08:35 AM
#3
Re: keyword extern
the short/incomplete version:
extern means you are telling the compiler to assume that some object of a certain type exists but that it shouldn't actually create this variable.
Some other .cpp file will declare (and create) the variable, or the variable will be part of an external linkage (.obj or .lib) you are linking with.
- The first case works, but is messy. It's better to define the variable in a .h and declare it (once) in one of the .cpp files.
- the 2nd case (external linkage) is the only 'clean' reason why you would have to use it.
-
December 10th, 2012, 07:10 PM
#4
Re: keyword extern
Hi Paul,
Could you explain the difference between using "extern" on a function verses a global function?
Say I create two files named Globals.h and Globals.cpp.
- I code a function in Globals.cpp.
- I place the function declaration in Globals.h.
- I insert "#include "Globals.h" at the start of every file that needs to use that function.
This would be a global function would it not?
Now I prefix the function declaration with "extern" in the Globals.h file.
What's the difference with or without the "extern" prefix?
Thanks,
Raptor
-
December 9th, 2012, 10:27 PM
#5
-
December 11th, 2012, 10:07 AM
#6
Re: keyword extern
Function names have external linkage by default, so there is no difference.
-
December 11th, 2012, 06:33 PM
#7
Re: keyword extern
Originally Posted by laserlight
Function names have external linkage by default, so there is no difference.
Thanks for that clarification.
Raptor
-
December 12th, 2012, 05:54 AM
#8
Re: keyword extern
except when you meant
extern "C"
In that case, the extra "C" means you will get external linkage using the C convention instead of the normal C++ convention (with mangling).
-
December 12th, 2012, 10:58 PM
#9
Re: keyword extern
Originally Posted by OReubens
except when you meant
extern "C"
In that case, the extra "C" means you will get external linkage using the C convention instead of the normal C++ convention (with mangling).
Thanks for your info.
Raptor
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
|