public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* extern functions in header file
@ 2004-09-10  6:37 Ming-Chia Jiang
  2004-09-10 10:40 ` Eljay Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Ming-Chia Jiang @ 2004-09-10  6:37 UTC (permalink / raw)
  To: gcc-help

Hi folks,

Is there any difference between declaring functions with prefix "extern" and 
without "extern" keyword in header files??
For example, I define a function FUNC(void, int) in test.c. And test.c 
includes the test.h with the following two conditions:

--test.h with extern--
extern void FUNC(void, int);

--test.h without extern--
void FUNC(void, int);

The output result seems no difference, and also the compiler does not show 
warning messages for both cases. Can anyone advise what the 'extern' keyword 
effects in the header file?

Best Regards
Ming-Chia

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: extern functions in header file
  2004-09-10  6:37 extern functions in header file Ming-Chia Jiang
@ 2004-09-10 10:40 ` Eljay Love-Jensen
  2004-09-10 11:04   ` Sriharsha Vedurmudi
  0 siblings, 1 reply; 3+ messages in thread
From: Eljay Love-Jensen @ 2004-09-10 10:40 UTC (permalink / raw)
  To: Ming-Chia Jiang, gcc-help

Hi Ming-Chia,

 >Is there any difference between declaring functions with prefix "extern" 
and without "extern" keyword in header files?

 From a C++ vantage point...

Effectively (by and large), no there's no difference.

Philosophically, yes.  Some consider (including myself) that supplying the 
(optional) <code>extern</code> keyword is good practice because it is 
self-documenting code.

Also, be aware that <code>extern "C"</code> has several important 
(critical!) implications regarding C++ code.

Also, in some areas, <code>extern</code> is REQUIRED to distinguish 
something that is "just a declaration" from being mistaken for a 
"definition" (without having the <code>extern</code>).  See Stroustrup's 
C++ Programming Language (3rd or special edition) section 9.2.

I haven't done C work since 1990, so my comments may not be appropriate for 
C "good style / best practices".

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: extern functions in header file
  2004-09-10 10:40 ` Eljay Love-Jensen
@ 2004-09-10 11:04   ` Sriharsha Vedurmudi
  0 siblings, 0 replies; 3+ messages in thread
From: Sriharsha Vedurmudi @ 2004-09-10 11:04 UTC (permalink / raw)
  To: gcc

This is what the C99 standard says:
==============================================================================================================
6.2.2 Linkages of identifiers *

*1 An identifier declared in different scopes or in the same scope more 
than once can be made to refer to the same object or function by a 
process called linkage. There are three kinds of linkage: external, 
internal, and none.

2 In the set of translation units and libraries that constitutes an 
entire program, each declaration of a particular identifier with 
external linkage denotes the same object or function. Within one 
translation unit, each declaration of an identifier with internal 
linkage denotes the same object or function. Each declaration of an 
identifier with no linkage denotes a unique entity.

3 If the declaration of a file scope identifier for an object or a 
function contains the storageclass specifier static, the identifier has 
internal linkage.

4 For an identifier declared with the storage-class specifier extern in 
a scope in which a prior declaration of that identifier is visible, if 
the prior declaration specifies internal or external linkage, the 
linkage of the identifier at the later declaration is the same as the 
linkage specified at the prior declaration. If no prior declaration is 
visible, or if the prior declaration specifies no linkage, then the 
identifier has external linkage.

5 If the declaration of an identifier for a function has no 
storage-class specifier, its linkage is determined exactly as if it were 
declared with the storage-class specifier extern. If the declaration of 
an identifier for an object has file scope and no storage-class 
specifier, its linkage is external.

6 The following identifiers have no linkage: an identifier declared to 
be anything other than an object or a function; an identifier declared 
to be a function parameter; a block scope identifier for an object 
declared without the storage-class specifier extern.

7 If, within a translation unit, the same identifier appears with both 
internal and external linkage, the behavior is undefined.
==============================================================================================================


Hope this clears up the things....

-Harsha.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-09-10 11:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-10  6:37 extern functions in header file Ming-Chia Jiang
2004-09-10 10:40 ` Eljay Love-Jensen
2004-09-10 11:04   ` Sriharsha Vedurmudi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).