Multiple defines error?

Discussion about everything. New games, 3d math, development tips...
Post Reply
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Multiple defines error?

Post by 3DModelerMan »

I get this error
CHavokCharacterController.obj : warning LNK4006: "char const * const engine::core::physics_typename" (?physics_typename@core@engine@@3PBDB) already defined in CHavokPhysicsAnimator.obj; second definition ignored
when I try to define variables in my header file like this

Code: Select all

#ifndef TYPES_H
#define TYPES_H

#include <Irrlicht.h>
#include "utils.h"

namespace engine
{
 namespace core
 {
  //Havok physics animator 
  const int ESNAT_HAVOK_PHYSICS = MAKE_IRR_ID('h','k','p','a');
  const char* physics_typename = "HavokPhysicsAnimator";
 }

 namespace events
 {
  const irr::u32 evtLoadNewLevel = utils::stringHash("LoadNewLevel");
 }
}

#endif
but it only happens when I include that file more than once. Does anyone know how to fix it? The include guards don't seem to be working.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Since you include the file in both .cpp files, the variable definition is contained in both places. Include guards only avoid duplicated inclusion into the same file, e.g. due to the same include statement in the .cpp file and its .h file. You have two completely separate .cpp files here, which create two separate .o files. And both will include the variable, which leads to the compile time problem and major problems due to wrong access to separate variables.
You must put definitions into .cpp files, or into classes.
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

You can do

Code: Select all

//Somefile.h
extern bool someVar;

Code: Select all

//Somefile.cpp
bool someVar;
That way it is defined however many times needed but declared only once.
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Post by 3DModelerMan »

I want to define it once, then use it all over the place, will that work with extern variables? Or will I have to redefine it every time?
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

If you redefine, you have the same problem as before. You just want to redeclare (foward declare) the variables, which can be done with the extern keyword. These variables are global, though, and hence on the dark side of programming techniques...
Post Reply