delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/04/01/17:34:21

Date: Fri, 01 Apr 1994 16:57:56 -0500 (EST)
From: "Wonkoo Kim, EE, U. of Pittsburgh" <WKIM AT vms DOT cis DOT pitt DOT edu>
Subject: Re: storage allocation of global var
To: dj AT ctron DOT com
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

>Date: Fri, 01 Apr 1994 10:42:25 -0500
>From: dj AT ctron DOT com (DJ Delorie)
>Subject: RE: storage allocation of global var
>
>> and, a global variable is declared in test.h:
>> 
>>   int global_var;
>
>This creates a "common" variable, space for which is shared among
>object files (like FORTRAN's "common" statement).  Had you given it a
>value in one module, that value would be set for all modules.  Had you
>given it a value in more than one module, you would have a link error.

What a nice linker!

I got several replies and thanks to all of them.

The global variable declaration without explicit 'extern' seems to be
supported by most C compilers (but not sure about C++) from the replies and 
my experience with djgpp, bcc, etc. [djgpp's support is approved by DJ :)]

This situation seems could be extended to function protocols in the same
sense.  K&R book said, in mutiple-file program, function definitions without
'extern' specifier should appear in *exactly* one of those source files, all 
other should have 'extern' specifier if the file does have external function 
calls.  But, djgpp does support _implicit_ 'extern' when I put function 
protocols to separate header files. 
I.e., I don't need to put 'extern' explicitly in function protocol 
definitions in *.h files even if they are #included in sources that make 
external calls.  At least, no problem with djgpp or bcc.

Is this a standard behavior of most C compilers?

Wonkoo Kim
wkim AT vms DOT cis DOT pitt DOT edu


P.S.
Previously, I always followed K&R and put 'extern' when it is external.
To put function protocols and global vars to *.h, I defined EXTERN in test.h:
 #ifndef _TEST_H_
 #define _TEST_H_
  ...
  #ifdef EXTERN
    #undef EXTERN
  #endif
  #ifdef _TEST_C_  /* defined only in test.c just before #include "test.h" */
    #define EXTERN
  #else
    #define EXTERN extern	/* for all except test.c */
  #endif
  EXTERN function_protocols(...);
  ...
  EXTERN global_var_definition;
  ...
 #endif		/* _TEST_H_ */

This was what I did until now.  But, if the 'implicit' extern for *.h is
supported by most C compilers, then I wasted my time to put the unnecessary
wrappers. :) 


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019