B
blangela
I am currently taking a course and one of the example programs showed
a function prototype something like:
extern void func();
I asked why the "extern" was necessary.
Someone in the class (not the instructor) explained that if the same
function prototype is used in more than one source file in a project,
that the linker will complain.
He explained to me that this was exactly the same as when you have a
global variable scoped in several source files, that where ever the
global variable is defined it does not need to be explicitly defined
as extern, but that in any other files that it needs to be in scope,
the global variable must be declared extern. This part I know to be
true from my own experience. It seemed to make sense, so I believed
him.
When I got back home I tried to verify what was stated above about
extern and function prototypes, but try as I might, using Visual
Studio Express 8, I could not generate any linking errors by leaving
out the extern keyword in function prototypes. BTW, I tried with both
a .c project (all the source files were .c files and I set the
compiler option to "Compile as C code" - the /TC option) and a .cpp
project (all the source files were .cpp files).
Can anyone give me an example where the extern keyword is actually
required? Or is Microsoft up to it's old tricks and "helping me"
without my knowledge and I would have had linking errors on other
IDEs ?
Bob
a function prototype something like:
extern void func();
I asked why the "extern" was necessary.
Someone in the class (not the instructor) explained that if the same
function prototype is used in more than one source file in a project,
that the linker will complain.
He explained to me that this was exactly the same as when you have a
global variable scoped in several source files, that where ever the
global variable is defined it does not need to be explicitly defined
as extern, but that in any other files that it needs to be in scope,
the global variable must be declared extern. This part I know to be
true from my own experience. It seemed to make sense, so I believed
him.
When I got back home I tried to verify what was stated above about
extern and function prototypes, but try as I might, using Visual
Studio Express 8, I could not generate any linking errors by leaving
out the extern keyword in function prototypes. BTW, I tried with both
a .c project (all the source files were .c files and I set the
compiler option to "Compile as C code" - the /TC option) and a .cpp
project (all the source files were .cpp files).
Can anyone give me an example where the extern keyword is actually
required? Or is Microsoft up to it's old tricks and "helping me"
without my knowledge and I would have had linking errors on other
IDEs ?
Bob