On Apr 16, 11:00Â pm, "Alf P. Steinbach"
wrote:
> * 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?
>
> 'extern' is the default for C++ function declarations, so ordinarily you do not
> need it.
>
> You need it only where it's not the default, and/or where you want to specify
> "C" linkage.
>
> An example where it's not default: the default for constants is internal
> linkage, so if you declare a constant in a header file and define it in an
> implementation file you need to use 'extern', like
>
>
> extern double const pi;
>
>
>
> #include "x.h"
> double const pi = 3.14;
>
>
Thanks for your reply. If I undertand you correctly, the use of
extern as I explained it in my question is _NEVER_ necessary in a C++
application?
What about in a C application? Is the same answer true?
>> Â 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 ?
>
> You mean compilers.
>
> An IDE is not a compiler: it just gives you easy access to a compiler, and other
> relevant tools.
To be honest, the reason I used IDE, was as a short cut for compiler
and linker, since I am not sure if it is the MS compiler or MS linker
that is the problem. Does that make sense?
Thanks again for your reply!