Archived

This topic is now archived and is closed to further replies.

Jonppe

error LNK2005:

Recommended Posts

Jonppe    122
When I try to link my program. I get 2 errors. StdAfx.obj : error LNK2005: _IID_IDirectDraw4 already defined in Init.obj and other of same type of error about _GUID_SysKeyboard and I only use them in funktions QueryInteface and CreateDevice. I have also included all libraries needed(I think so) in stdafx.h .. so what to do? Thanks for your help.

Share this post


Link to post
Share on other sites
JoeyBlow2    100
That problem is because of this:

        
CPP ------ H ----- Global Variables
/
/
CPP ------



You are having to CPP files using 1 header file... This 1 header file is creating your global variables. This is generally a no-no... Why? Because the linker error you just got.

When the compiler linker is making code for the CPP... It says "Ok, I gotta make this global variable because its defined in this header file" then it gets to the next CPP file and says "Ok, I gotta make this global variable because its defined in this header file"... So now you have the global variable made twice!!! This is what the linker error is reporting. How do you fix it?

Easy. Only make the global variable once... But you can define it in the header file. What you do is createa another CPP file which has your global variables defined... and declare all global variables in the header file as "extern"...



Variables--
CPP--------- Header
/
CPP------




That is one way, or you can dynamically skip the variable creation... Here is probably what it looks like now:

        

struct _mystruct
{

short myvar;
} GlobalStruct;



Having GlobalStruct on the bottom is creating that global variable. You don't want that in a header file. You can have this dynamic skip by doing something like this:



struct _mystruct
{

short myvar;
}

#ifndef _ALREADYDEFINED_
#define _ALREADYDEFINED_

_mystruct GlobalStruct;

#endif



That way the _mystruct is always defined for all CPP's using this header, but only the first time this code is being ran through the compiler will acutally make the global variable "_mystruct GlobalStruct"... Because the first time its being run through the compiler the _ALREADYDEFINED_ is not defined... Then it defines it and creates the variable. So, next time the compiler is going through this header file (because another CPP file), things are already defined so it skips creating that variable... but the compiler knows the structure of the struct, which is all that is really needed...

Does that make sense?

Edited by - JoeyBlow2 on October 31, 2000 3:43:09 PM

Edited by - JoeyBlow2 on October 31, 2000 3:44:03 PM

Edited by - JoeyBlow2 on October 31, 2000 3:47:04 PM

Share this post


Link to post
Share on other sites
SiCrane    11839
This could be because you''re defining INIT_GUID and linking against dxguid.lib. If you define INIT_GUID, you should do it in only one implemenation file, and not link against dxguid.lib.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
BigCarlito: While your suggestion is a good one, it provides no solution to the linker error Jonppe describes. It will only prevent the compiler from including the same header file twice in the __same__ CPP-file (which would produce a compiler error, not a linker error). It will not prevent the compiler from including a header file in __different__ CPP-files.

The IID_IDirectDraw4 and GUID_SysKeyboard are declared in the DX-headers.

As someone said above: linking your project to dxguid.lib is the simplest method to avoid linker-clashes.

And it is supposed to be INITGUID, not INIT_GUID. The important thing is to not define INITGUID in more than one CPP-file in a project (and of course not link to the dxguid.lib if you do it the INITGUID way).

Share this post


Link to post
Share on other sites