Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2005, 10:14 AM   #1
some1
Programmer
 
Join Date: Dec 2004
Posts: 50
Rep Power: 4 some1 is on a distinguished road
Including header files

How to prevent header files from being included twice?
My code is messed up and I need to include lots of .h files in other files so I get errors from the linker that X is already defined in x.obj file.

I tried using inclusion guards like this:

#ifndef INC_HEADER1
#define INC_HEADER1

<file content goes here>

#endif

and also tried

#pragma once

but i still get the same errors
But even if i include e.g windows.h or string.h a hundred times i never get linker errors about them.

help?
some1 is offline   Reply With Quote
Old Apr 27th, 2005, 2:45 PM   #2
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
I'm not sure but I think only one compiled version of any library you include goes into the final product. I may be confusing this with a feature of another C-type language, though.
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old Apr 27th, 2005, 3:47 PM   #3
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
A header should only have declarations. It sounds like you are trying to define something multiple times outside of the headers. Definitions of shared names should be placed in a separate source file so as to ensure that they are only defined once:
/* defs.h */
#ifndef HEADER
#define HEADER

/* Declarations */

#endif
/* defs.c */
#include "defs.h"

/* Definitions */
Compile and link the source file along with your project, and all should be well.
Eggbert is offline   Reply With Quote
Old Apr 27th, 2005, 5:16 PM   #4
some1
Programmer
 
Join Date: Dec 2004
Posts: 50
Rep Power: 4 some1 is on a distinguished road
actually its a global HBRUSH that is used by the functions in the file i`m trying to include. I`m not getting errors for the functions but the global HBRUSH only (which is the only global in that file). Should I not put variables in header files?

What I`m making is an application divided into 7 parts. they`re big parts so I`m dividing each into a seperate .cpp & .h file plus a main .cpp & .h file. Dunno if thats ok. I`m a noob afterall .

Any tips?
some1 is offline   Reply With Quote
Old Apr 27th, 2005, 5:26 PM   #5
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>Should I not put variables in header files?
Variable declarations, yes. Not variable definitions. To adjust my previous example:
/* defs.h */
#ifndef HEADER
#define HEADER

extern HBRUSH brush;

#endif
/* defs.c */
#include "defs.h"

HBRUSH brush;
Eggbert is offline   Reply With Quote
Old Apr 27th, 2005, 6:11 PM   #6
some1
Programmer
 
Join Date: Dec 2004
Posts: 50
Rep Power: 4 some1 is on a distinguished road
thanks for helping

i have 2 questions:

1. What does the keyword extern mean?
2. What is the difference between variable definition and declaration?
some1 is offline   Reply With Quote
Old Apr 27th, 2005, 9:05 PM   #7
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>1. What does the keyword extern mean?
As used, it means that the name is defined elsewhere.

>2. What is the difference between variable definition and declaration?
A declaration tells the compiler that a name exists. A definition allocates storage for that name and optionally initializes the storage with a value.
Eggbert is offline   Reply With Quote
Old Apr 28th, 2005, 8:35 AM   #8
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 328
Rep Power: 4 mackenga is on a distinguished road
Just a little something to add; when you're making sure your header doesn't get included twice using ifndef and define, sometimes defining a constant without a value isn't a hot idea (depends on the compiler and any other software you have parsing the program):

#ifndef _HEADER_FILE_H_
#define _HEADER_FILE_H_ 1

/* ... declarations here ... */

#endif /* _HEADER_FILE_H_ */

Obviously, don't use _HEADER_FILE_H_ as the name of your constant, but something more directly related to what you're working on and what the header file contains/is for. The _HEADER_FILE_H_ in the comment beside #endif is not strictly necessary but aids readability.

This means even if you do declare and define variables within the header file, as long as it's between the #ifndef and the #endif that matches it, that code will only be processed beyond the preprocessor once so it'll work.

If you have global variables, though, declaring them extern in the header and putting them in a file called globals.c or whatever is often a good idea. Global variables can be hard enough to keep track of after all.
mackenga is offline   Reply With Quote
Old Apr 28th, 2005, 9:59 AM   #9
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>sometimes defining a constant without a value isn't a hot idea
Only when working with legacy code and very old compilers. Any compiler that correctly supports ISO C89 should have no trouble with it.

>Obviously, don't use _HEADER_FILE_H_ as the name of your constant, but
>something more directly related to what you're working on
Don't use a name with a leading underscore for this use either, as that invades the implementation's name space.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:19 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC