problems using one header file for multiple source files 
Author Message
 problems using one header file for multiple source files

Hello. Oh, I just know I'm going to look like an idiot for
asking such a stupid question, but I have a single .h file
that includes other files, makes global variables, and sets
up structures. If I write all my functions into one .c file
and include said .h file, everything works fine. If I split
my C code over several files, including the .h file in each
causes numerous compilation errors having to do with variables
being declared over again.

What the heck's wrong?! For the record, the .h file does have
an #ifndef to test if it's been declared already:

#ifndef _MYHEADER_H
#define _MYHEADER_H

[declare stuff]

#endif

Thanks in advance,
jasonp



Fri, 02 Jun 2000 03:00:00 GMT  
 problems using one header file for multiple source files

A global object must be declared exactly once.  Then it can be referenced as
many times as you like.  In the header files, to reference global data items,
you should have something like this:

/* Globals.h external symbol import definitions: */
/* Since I don't want to work very hard to get rid */
/* of this crap, I will just make public symbols. */
#define MAX_STRING_LEN 1024;
extern int gMyCrappyGlobalInteger;
extern char gMyCrappyGlobalChar;
extern double gMyCrappyGlobalDouble;
extern float gMyCrappyGlobalfloat;
extern char gMyCrappyGlobalString[MAX_STRING_LEN+1];

Don't really use beastly, ugly names like that.  I did it just to be annoying.

Then you will have one source file called globaldefs.c

/* Globals.c external symbol declarations: */
#include "globaldefs.h"
int gMyCrappyGlobalInteger;
char gMyCrappyGlobalChar;
double gMyCrappyGlobalDouble;
float gMyCrappyGlobalfloat;
char gMyCrappyGlobalString[MAX_STRING_LEN+1];

Include the file globals.h into all the source modules that need to use them.
If this does not answer your question, perhaps you can clarify the actual
errors you are receiving.
Globals should only be used when absolutely necessary.  They are evil, when
used willy-nilly.  When I see a big pile of globals, I begin to whinge.  I
envision Scott Nudds blasting out of my right nostril with very little
provocation, and then forcing me to write all future projects in PASM.
--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com

Quote:

>Hello. Oh, I just know I'm going to look like an idiot for
>asking such a stupid question, but I have a single .h file
>that includes other files, makes global variables, and sets
>up structures. If I write all my functions into one .c file
>and include said .h file, everything works fine. If I split
>my C code over several files, including the .h file in each
>causes numerous compilation errors having to do with variables
>being declared over again.

>What the heck's wrong?! For the record, the .h file does have
>an #ifndef to test if it's been declared already:

>#ifndef _MYHEADER_H
>#define _MYHEADER_H

>[declare stuff]

>#endif

>Thanks in advance,
>jasonp



Sat, 03 Jun 2000 03:00:00 GMT  
 problems using one header file for multiple source files



Quote:
> Hello. Oh, I just know I'm going to look like an idiot for
> asking such a stupid question, but I have a single .h file
> that includes other files, makes global variables, and sets
> up structures. If I write all my functions into one .c file
> and include said .h file, everything works fine. If I split
> my C code over several files, including the .h file in each
> causes numerous compilation errors having to do with variables
> being declared over again.

> What the heck's wrong?! For the record, the .h file does have
> an #ifndef to test if it's been declared already:

> #ifndef _MYHEADER_H
> #define _MYHEADER_H

> [declare stuff]

> #endif

> Thanks in advance,
> jasonp

Here is one possible technique:

Header file:

===================
#ifndef _MYHEADER_H
#define _MYHEADER_H

#ifndef GLOBAL
#define GLOBAL extern

GLOBAL int i;
GLOBAL int j;
GLOBAL int k;

#endif /* _MYHEADER_H */
===================

One source file only (in large projects, this is usually a file
called "globals.c" or "vars.c", and does not contain any code,
or anything other than shown below):

===================
#define GLOBAL /* */
#include "myheader.h"
===================

Defining GLOBAL to be a comment causes the variables in
myheader.h to be defined and allocated in this one source file.
Other source files do not contain the definition for GLOBAL
before the include, so they see variable declarations:

extern int i;
extern int j;
extern int k;

Some people don't like this technique.  The alternate is to
repeat all the variable types in a source file to define them,
but they you must be careful to make all changes to both files.

Jack



Mon, 05 Jun 2000 03:00:00 GMT  
 problems using one header file for multiple source files


Quote:
> Hello. Oh, I just know I'm going to look like an idiot for
> asking such a stupid question, but I have a single .h file
> that includes other files, makes global variables, and sets
> up structures. If I write all my functions into one .c file
> and include said .h file, everything works fine. If I split
> my C code over several files, including the .h file in each
> causes numerous compilation errors having to do with variables
> being declared over again.

Make each global the "property" of one .c file, and move its
definition into that file.  If it's only accessed in that one
file, make it static.  Otherwise, consider making it static
anyway, and passing it to other files via parameters;  if you
can't do that, put an extern declaration for it in the header.

--



Mon, 05 Jun 2000 03:00:00 GMT  
 problems using one header file for multiple source files

Groovy hepcat Jason Stratos Papadopoulos was jivin' on 15 Dec 1997
05:24:00 GMT in comp.lang.c.
problems using one header file for multiple source files's a cool
scene! Dig it!

Quote:
>Hello. Oh, I just know I'm going to look like an idiot for
>asking such a stupid question, but I have a single .h file
>that includes other files, makes global variables, and sets

  When you say "makes global variables", what exactly do you mean? If
you mean declares them, then it should be fine. If, OTOH, you mean
defines them, then you've got problems!
  Judging by the fact that you've got problems, and the specific
nature of those problems, I'd say it's the latter.

Quote:
>up structures. If I write all my functions into one .c file
>and include said .h file, everything works fine. If I split
>my C code over several files, including the .h file in each
>causes numerous compilation errors having to do with variables
>being declared over again.

  Uh huh. Common error. But they're *defined* over again, not just
declared over again.

Quote:
>What the heck's wrong?! For the record, the .h file does have

  You're defining variables within the header, such as:

int foo;
float bar;

The above lines define two variables with external linkage (since
they're outside of any function definitions). They set asside space
for the variables. If the header is included in multiple modules, it
gets compiled multiple times, and the variables are defined multiple
times. When the linker tries to link the modules together, it
encounters multiple definitions of the same variables, and spits out
an error message.
  What you should be doing is *declaring* them, not defining them. You
must also define them in one .c file once.

  How to declare a variable in a header (using the same variables used
in the example above):

extern int foo;
extern float bar;

It's as simple as that. Simply prepend extern to the variable
declarations, which tells the compiler the variables are defined in
another source file.
  An extern declaration can appear in the same translation unit (even
the same file) as the definition, though. So this is valid:

/* myfile.h */
extern int foo;
extern float bar;
/* end */

/* myfile.c */
#include "myfile.h"
int foo;
float bar;
/* end */

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Sun, 11 Jun 2000 03:00:00 GMT  
 problems using one header file for multiple source files



Quote:
>Hello. Oh, I just know I'm going to look like an idiot for
>asking such a stupid question, but I have a single .h file
>that includes other files, makes global variables, and sets
>up structures. If I write all my functions into one .c file
>and include said .h file, everything works fine. If I split
>my C code over several files, including the .h file in each
>causes numerous compilation errors having to do with variables
>being declared over again.

>What the heck's wrong?! For the record, the .h file does have
>an #ifndef to test if it's been declared already:

>#ifndef _MYHEADER_H
>#define _MYHEADER_H

>[declare stuff]

>#endif

This only protects you against the header being included more than one in the
same translation unit. The only way in which things in different translation
units can interact is through a process called linkage (and specifically
external linkage here). Macro identifiers don't have linkage.

Also note that identifiers styarting with and underscore followed by an
uppercase letter are always reserved for use by the implementation therefore
you should never define them in your code (and that includes user-defined
headers). I suggest you remove the leading underscore in this case.

--
-----------------------------------------


-----------------------------------------



Wed, 14 Jun 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Using one header file for several source files - help please

2. How to use one header file with several source files (again)

3. using header file not with source vc++6.0

4. Multiple file source code problem

5. instantiating a object in one file but using it in another file

6. instantiating a object in one file but using it in another file

7. C Header files including header files, mutually referencing typedefs

8. Problem: Using header files

9. problem getting header file for the COM dll that uses msado15.dll

10. Trouble Getting multiple files using Common File Dialog

11. STL header files vs non-STL header files

12. #ifndef/#ifdef problems in my header file (conditional header inclusion)

 

 
Powered by phpBB® Forum Software