Click to See Complete Forum and Search --> : redefinition of xxx ignored


Andreas Richter
March 31st, 1999, 12:12 AM
A very simple question of all, I think. But no idea myself :-(

I have written a cpp-prg which´s source is getting too long now. I have build modules of it and included the necessary .hpp - files in each module. During compilation all is ok, but when the linker starts it bombs me with hundreds of "redefinition of xxx ignored in xxx". This are only warnings, no errors, and the linked prg is ok. Reduciong the warning - level does not change anything. It is not a really problem, but I want to kill this warning - list. If I build a new .hpp - file for each module, I must define each variable with "extern unsigned long..." once more, so that changing of one variable takes changing of 20 .hpp - files.

What must I do?

Wing
March 31st, 1999, 02:05 AM
Hello Andreas,


if you have defined a variable, say int i as global in one module, then you cannot define it again in another module. You should define it external, that's the reason for the warnings. You can define a compiler variable in your main module like this:


#define MAIN


Then you can change your hpp files as follow to define your global variables:


#ifdef MAIN

int i;

#else

extern int i;

#endif


Then you just need to change the hpp-file.


Greetings

Wing.

Bore
March 31st, 1999, 07:59 AM
Now might be a good time to re-think your overall design. There is one school of object oriented thought that says you should try to avoid globals. Of course, it would take a while to fix your problem, but you might be saving yourself from future woes.

Gomez Addams
March 31st, 1999, 01:27 PM
Mr. Bore has a very valid point - the use of global data should

be minimized. When I need globals I use a variation of Wing's

approach. In a header somewhere do this :


#ifndef GLOBAL

#ifdef MAIN

#define GLOBAL

#else // MAIN

#define GLOBAL extern

#endif // MAIN

#endif // GLOBAL


Then when you declare a variable that will be global do this :


GLOBAL MyGlobalData MyData;


Then, in one and ONLY one module, typically the CWinApp derivation

module, define MAIN before including the header with the globals :


#define MAIN

#include "header.h"


This will work for nearly any kind of data including the

application object.