View Single Post
Old Nov 13th, 2005, 9:22 AM   #1
some1
Programmer
 
Join Date: Dec 2004
Posts: 50
Rep Power: 4 some1 is on a distinguished road
Including files and inclusion guards

Hello everyone!

I've been having this annoying problem for a while and I cant sort it out.

I have file "globals.h" where I store all my global vars in. I also have 2 other files main.cpp and code2.cpp where I write my code.
The problem is, when I #include "Globals.h" in both main.cpp and code2.cpp it gives me this error:
fatal error LNK1169: one or more multiply defined symbols found

So I tried using inclusion guards like this:
#ifndef _INC_GLOBALS
#define _INC_GLOBALS
....variable definitions here.....
#endif

but it keeps giving me the same error.
if I remove the #include "Globals.h" from one of the files it just says that variable x is not defined.

I used #pragma message to see how many times the linker uses the file and it outputs my msg twice. So here is a sample application I built just to test this:

Main.cpp
#include <iostream.h>
#include "globals.h"

int main(int argc, char *argv[])
{
	x = 123;

	return 0;
}

code2.php
#include "globals.h"

void DoSomething();

void DoSomething()
{
	x = 456;
}

Globals.h
#ifndef _INC_GLOBALS
#define _INC_GLOBALS
#pragma message("Including globals.h")

int x = 0;

#endif

I also tried using "#pragma once" but nothing changes.

Here is the build output

Quote:
Originally Posted by Build output
Compiling...
code2.cpp
Including globals.h
main.cpp
c:\Program Files\Microsoft Visual Studio .NET\Vc7\include\useoldio.h(29) : warning C4995: '_OLD_IOSTREAMS_ARE_DEPRECATED': name was marked as #pragma deprecated
Including globals.h
Generating Code...
Linking...
code2.obj : error LNK2005: "int x" (?x@@3HA) already defined in main.obj
Debug/InclusionGuards.exe : fatal error LNK1169: one or more multiply defined symbols found
Note that the "Including Globals.h" message appears twice.

I also dont understand why, when even if I include e.g. windows.h or iostream.h 100 times in the same file I never get such errors while they use the same inclusion guard method...



In desperate need of help,
Some1
some1 is offline   Reply With Quote