   Header files + linking
  I have a header file / linker problem that has been frustrating me for the last three hours.Basically I have four .cpp/.h files in a project, all of which use ddraw.h. Because I thought it would be an good idea, I put all my macros, includes into the one header file called global.h - #ifndef _GLOBAL_#define _GLOBAL_//window includes#include <windows.h>#include <mmsystem.h>#include <stdio.h>#include <stdlib.h>//direct X includes#include <ddraw.h>//game engine includes#include "resource.h"#include "Sprite.h"#include "Shell.h"#endifThen in Sprite.cpp and Shell.cpp and Main.cpp the only include I have is: #include "global.h"But when I try to build the application I keep getting 33 directdraw errors : Shell.obj : error LNK2005: _IID_IDirectDrawGammaControl already defined in Main.objand so on. I have tried removing ddraw.h from the global file and including it indivually in the .h files.Also tried building without the global file. Any ideas? 
  Did you forget to remove #include "ddraw.h" in main.cpp?  Maybe it is not at the top of the file? ;  Well, first of all, this is a link error, not a compilation error. So you''re use of #includes is fine as far as the compiler and you''re code is concerned.The problem here is that someone is creating a global called"_IID_IDirectDrawGammaControl" in one of the header files you''re including. Since each compilation unit (.cpp file) in you project is including the same set of files (via you''re global.h include), you are getting a linker error because a variable with the same name is being defined in each of your source files.Now, since I find it hard to believe that ddraw.h or any of the other standard windows includes would do that, I suspect that to culprit is://game engine includes#include "resource.h"#include "Sprite.h"#include "Shell.h"The solution to this problem depends upon whether you wrote this game engine. If you wrote this engine you should simply grep for the definition of this variable and and an "extern" keyword in front of it and then actually define it in an engine .cpp file. This way other .cpp files that include this header will know about this variable, and be able to use it (if only one .cpp file actually uses this variable then I question why it is even defined in a header file), but wont try to create storage for it at compile time. If you did not write this engine, you may be stuck with having to only include it''s headers in one compilation unit.BTW, it is in general evil to ever include files that you don''t really need in a .cpp file, so most occurance of someone putting all of their includes in 1 include file is a sign of bad/sloppy/lazy coding). In the long run, you will have tremendously slow compile times with this strategy, and you will defeat you''re "make" system as everything is dependant upon everything else.   Tony ;  Nope. IID_IDirectDrawGammaControl is not declared/used in any of my files. And they are 33 linker errors.Shell.obj : error LNK2005: _IID_IDirectDrawSurface7 already defined in Main.objShell.obj : error LNK2005: _IID_IDirectDrawSurface4 already defined in Main.objShell.obj : error LNK2005: _IID_IDirectDrawSurface3 already defined in Main.objShell.obj : error LNK2005: _IID_IDirectDrawSurface2 already defined in Main.objShell.obj : error LNK2005: _IID_IDirectDrawSurface already defined in Main.objetc ;If you grep through the files does it appear at all?are you doing:struct _foo{  int a, b;} IID_IDirectDrawGammaControl;If no, you need to track down where this thing is being defined (even if it is in ddraw.h) and post what that looks like for me to help anymore...   Tony ;  Please go through this article.It might just help!    [edited by - hehenoobhehe on August 1, 2003 9:36:27 PM];  Managed to fix it myself after getting inspiration from a cup of coffee and two smack bars. That article looks like its well worth a read though.Thanks. ;quote:Original post by theObserver  Managed to fix it myself after getting inspiration from a cup of coffee and two smack bars. That article looks like its well worth a read though.Thanks.   How did you fix it? I''m interested in knowing because at first glance your code looked quite ok.!Thanks...