|
|
C Quellcode |
1 2 |
#ifndef MY_HEADERNAME_H #define MY_HEADERNAME_H |
|
|
C Quellcode |
1 |
|
Quoted from ""darthdespotism""
Die Lösung hier nent sich Include-Wächter.
Einfachstes Beispiel:
an den Anfang jeder Headerdatei schreibst du
![]()
C Quellcode
1 2 #ifndef MY_HEADERNAME_H #define MY_HEADERNAME_H
und ans Ende
![]()
C Quellcode
1#endif
Dabei natürlich MY_HEADERNAME_H durch den Namen des jeweiligen Headers ersetzen - Großbuchstaben werden dabei normalerweise Verwendet da defines.
CodeBlocks mach das beispielsweise automatisch wenn man die Funktion verwendet![]()
|
|
C Quellcode |
1 2 |
#include "a.h" #include "a.h" |
Quoted from ""d0nUt""
Welcome back Blue ;-)
Habe mal wieder etwas in alten Postings recherschiert...
Inzwischen Bachelor-Student der Allgemeinen Informatik in Bocholt?
Ihr macht DirectX? Wir machen OpenGL *freu* (Medieninformatik, Wiesbaden)
[...]


Quoted from ""darthdespotism""
Im Prinzip einfach: Wenn das makro noch nicht gesetzt wird, setzt man es und bindet den inhalt des Headers ein. Sonst macht man (der Compiler!) einfach nichts.
Das Problem kommt ja daher dass du manche Header sonst mehrfach einbindest, da du sie in verschiedenen unabhängigen Teilen brauchst oder so. Durch die Include-Wächter ist dann aber sichergestellt dass der Compiler den Inhalt nur einmal übernimmt. auch wenn du sowas schreiben würdest:
![]()
C Quellcode
1 2 #include "a.h" #include "a.h"
|
|
C Quellcode |
1 |
1>main.obj : error LNK2005: "int res_x" (?res_x@@3HA) already defined in CDirect3D.obj |
Quoted
On Jun 27, 9:33 am, Bram Kuijper <a.l.w.kuij...@rug.nl> wrote:
> >
> > long int idum;
> > long int idum2;
Your header guards are insufficient to prevent double definition
errors because there is more than one compilation unit including the
header. Each .cpp which includes the header will define its own
globals. The header guard only prevents a single compilation unit
from including the header more than once. What you need to do is
something like one of the following...
suggestion 1: Change the declarations in your header to:
extern long int idum;
extern long int idum2;
Then actually define these guys in one of your .cpp files.
suggestion 2: Modify your header to something like:
#ifdef DEFINE_GLOBALS
#define GLOBAL
#else // !DEFINE_GLOBALS
#define GLOBAL extern
#endif
GLOBAL long int idum;
GLOBAL long int idum2;
One of your .cpp files should define "DEFINE_GLOBALS" prior to
including this header. The only real advantage of this method over
the 1st is that you are then guaranteed that your definition and
declarations match.
Suggestion 3: Encapsulate the globals into one .cpp and provide
accessors to access/set the globals. This one is a bit of fuss, so I
don't like it so much, but...
long get_idum();
set_idum(long);
long get_idum2();
set_idum(long);
then in a .cpp you define:
long int idum;
long int idum2;
long int get_idum()
{
return idum;
}
void set_idum(long int v)
{
idum = v;
}
.
.
.
You should also consider putting your globals in your own namespace so
that they can't conflict with any other globals in the system.
Hope that helps,
joe