3
votes

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.

2

2 Answers

4
votes

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).

1
votes

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.