Click to See Complete Forum and Search --> : #IFNDEF Preprocessor problem...HELP!


quantass
December 23rd, 2002, 07:40 PM
I dont get it. I'm using MS VC++ 6 and have create 2 .CPP files and a single .H header file. Both .CPP files include the header file. The header file's contents are as follows:
---
#ifndef __FOGHORN__
#define __FOGHORN__

int x;

#endif
---

On compiling i get the error message:
---
file2.obj : error LNK2005: "int x" (?x@@3HA) already defined in file1.obj
Debug/file1.exe : fatal error LNK1169: one or more multiply defined symbols found
---

It seems the #IFNDEF preprocessor directive isnt functioning. Or maybe Im thinking it supposed to do something it's not. When I use the #IFNDEF/#DEFINE as I do in the header above isnt the preprocessor supposed to "remember" the new #DEFINE across the scope of the entire compiling process for all files in the project so that when the same #include file is included in another .CPP file the contents of the #IFNDEF/#ENDIF will NOT be included affectively avoiding an "already included" linking error? Or is it only effective during the compiling of a single .CPP file then when compiling begins for another the preprocessor clears its table starting fresh?

Is this how it works??

TheCPUWizard
December 23rd, 2002, 08:39 PM
No, the #ifndef is supposed to protect against

#include <something.h>
#include <morestuff.h>
#include <something.h> // Duplicate include in SAME unit.


You want to externally define the int in the header, then implement it in a single CPP.


file: me.h
extern int x;


file: my.cpp
#include "me.h"
int x;