![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2004
Posts: 50
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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;} |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
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 */ |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Dec 2004
Posts: 50
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>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; |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Dec 2004
Posts: 50
Rep Power: 4
![]() |
thanks for helping
i have 2 questions: 1. What does the keyword extern mean? 2. What is the difference between variable definition and declaration? |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>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. |
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 328
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|