3

Well, I have been struggling with this for days now. I am writing a custom game DLL for CryENGINE from scratch, and I cannot even get the solution compile with one simple class (Game.cpp) and a precompiled header (StdAfx.h).

Both Game.cpp and StdAfx.cpp will compile perfectly on their own, but compiling the solution throw tons of multiply defined errors. The class is simple because the definitions are just placeholders.

Game.h

#if !defined __GAME__H__
#define __GAME__H__

#pragma once

class CGame : public IGame
{
public:
CGame();
VIRTUAL ~CGame();

//IMPLEMENT: IGame Interface, all methods declared.
};
#endif

Game.cpp

 #include "StdAfx.h" //PreComp header
 #include "Game.h"

 //Define all methods, each one has a simple definition.

StdAfx.h

#if !defined __STDAFX__H__
#define __STDAFX__H__

#pragma once

//Various CryENGINE includes

#endif

Output

error LNK2005: "struct SSystemGlobalEnvironment * gEnv" (?  gEnv@@3PEAUSSystemGlobalEnvironment@@EA) already defined in StdAfx.obj
error LNK2005: "public: static long volatile _CryMemoryManagerPoolHelper::allocatedMemory" (?allocatedMemory@_CryMemoryManagerPoolHelper@@2JC) already defined in StdAfx.obj
error LNK2005: "public: static long volatile _CryMemoryManagerPoolHelper::freedMemory" (?freedMemory@_CryMemoryManagerPoolHelper@@2JC) already defined in StdAfx.obj
error LNK2005: "public: static long volatile _CryMemoryManagerPoolHelper::requestedMemory" (?requestedMemory@_CryMemoryManagerPoolHelper@@2JC) already defined in StdAfx.obj
error LNK2005: "public: static int volatile _CryMemoryManagerPoolHelper::numAllocations" (?numAllocations@_CryMemoryManagerPoolHelper@@2HC) already defined in StdAfx.obj

The list goes on...

What really throws me off is that each one will compile just fine individually, so syntax and references are good. What could possibly cause multiply defined errors when the solution is compiled as a whole?

I really appreciate help on this frustrating issue, thank you.

SvalinnAsgard
  • 225
  • 3
  • 10

2 Answers2

4

I'm not sure the errors are caused by the precompiled header, but here is the correct way to set up the precompiled header:

  1. Right-click on the project name in Solution Explorer, select Properties, go to Configuration Properties | C/C++ | Precompiled Headers and set the Precompiled Header setting to Use (/Yu). Leave the other two settings below it to the default.

  2. Right-click on StdAfx.cpp, go to the same setting and set it to Create (/Yc).

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • Thank you for the reply. That is exactly how I have it setup; I followed a really popular tutorial for it. I am not sure if it is caused by the header itself, or something else. I am leaning toward something else, but I have no idea what. – SvalinnAsgard Feb 06 '13 at 09:06
  • Where are the variables and methods in the linker error messages defined? Maybe they are defined in a header you include in StdAfx.h. – user1610015 Feb 06 '13 at 09:12
  • Some methods are implementations of interfaces, and those interfaces, like IGame above, are in the precompiled header... does that matter? Those methods in the error are defined deep in the CryENGINE source somewhere. – SvalinnAsgard Feb 06 '13 at 09:23
  • You have to put method implementations in a .cpp file (unless you implement them right inside the class definition like in Java and C#, but you should only do that for very small methods). – user1610015 Feb 06 '13 at 09:32
  • Game.h only has declarations, and then Game.cpp has the definitions in standard C++ fashion. However, Game.h implements IGame which is a CryENGINE provided interface. That interface is in the precompiled header, but Game.h is not. I hope that makes sense--I would post the entire code but it is really long. – SvalinnAsgard Feb 06 '13 at 09:39
  • Then I don't know what's wrong :( Try disabling the precompiled header setting completely to see if it has something to do with precompiled headers or not. – user1610015 Feb 06 '13 at 09:50
  • I just tried disabling precompiled headers. I left the same file intact, but now it is treated like a normal .h file. Unfortunately, my 32 multiply defined symbols went to 206. Game.cpp is now my only code, and it compiles perfectly on its own. However, the solution, containing this one and only class, does not. – SvalinnAsgard Feb 06 '13 at 09:59
  • What if you just remove StdAfx.h and StdAfx.cpp from the project and just include everything you need in Game.h so that there's a single .cpp file? – user1610015 Feb 06 '13 at 11:01
  • That works, but I get quite a few syntax errors from including everything from StdAfx.h. Because my class is so simple right now, the bare minimum will work, but trying to complete the entire game dll without a precompiled header will be slow and very messy. And, there is still the problem that this should not happen in the first place. Clearly, the error is on my end, but I am not sure where. – SvalinnAsgard Feb 06 '13 at 12:44
  • Thanks for the help, I think it is fixed--I made an answer about it. – SvalinnAsgard Feb 06 '13 at 14:16
2

Well, I figured it out. There is a clever complex include that does not belong in the precompiled header:

#include <Platform_Impl.h> 

This was causing all my problems, and by moving it to Game.cpp, everything is fine.

SvalinnAsgard
  • 225
  • 3
  • 10