3 Replies - 8827 Views - Last Post: 27 March 2010 - 10:39 AM

#1 Guest_dan99iel*


Reputation:

Help with Duplicate Symbol error in C++ / Objective C code

Posted 27 March 2010 - 07:56 AM

Hey there,

I'm having trouble with some code. I'm writing some O.O. code in C++ and Objective C (Using the .mm file format) and when I try to compile, it creates a linking error where it detects duplicate symbols. The code I'm about to show is of the Controller.h and Controller.mm files, and it's a class of a shape that will be made. I don't want to include this in the main file because I need to know how to do make other class files, and I don't want lots and lots of classes cluttering up the main file.

It DOES HAVE external libraries being imported here, but don't get confused. None of the code inside the functions are relevant. Even without the code in them compiling still gives the same error. The error is by the way:
ld: duplicate symbol ControllerClass::makeController(b2World*) in /Users/Daniel/Documents/XCode/2dFirst/build/Debug-iphonesimulator/libcocos2d libraries.a(Controller.o) and /Users/Daniel/Documents/XCode/2dFirst/build/2dFirst.build/Debug-iphonesimulator/2dFirst.build/Objects-normal/i386/Controller.o

The code is:
Controller.h
#import "Box2D.h"
#import "cocos2d.h"

class ControllerClass {
	b2PolygonShape dynamicBox;
	b2BodyDef bodyDef;
	b2FixtureDef fixtureDef;
	
public:
	b2World* world;
	b2Body* body;
	b2Fixture* fixture;
	CGPoint makeFloorPoint;
	CGPoint makeControllerPoint;
	void makeFloor(b2World* world);
	void makeController(b2World* world);
};


Controller.mm
#define PTM_RATIO 32

#include "Controller.h"

void ControllerClass::makeFloor(b2World* world) {
	makeFloorPoint = CGPointMake(160,0);
	
	//Make the body
	bodyDef.type = b2_kinematicBody;
	
	//Position set
	bodyDef.position.Set(makeFloorPoint.x/PTM_RATIO, makeFloorPoint.y/PTM_RATIO);
	body = world->CreateBody(&bodyDef);
	
	//Make a polygon shape
	dynamicBox.SetAsBox(5.0f, 0.1f);
	
	fixtureDef.shape = &dynamicBox;
	fixtureDef.density = 0.3f;
	fixtureDef.friction = 0.3f;
	fixtureDef.isSensor = true;
	fixture = body->CreateFixture(&fixtureDef);
}

void ControllerClass::makeController(b2World* world) {
	makeControllerPoint = CGPointMake(140, 160);
	//Make the body
	bodyDef.type = b2_dynamicBody;
	
	//Position set
	bodyDef.position.Set(makeControllerPoint.x/PTM_RATIO, makeControllerPoint.y/PTM_RATIO);
	body = world->CreateBody(&bodyDef);
	
	//Make a polygon shape
	dynamicBox.SetAsBox(.75f, .75f);
	
	//Fixture defenition and creation
	fixtureDef.shape = &dynamicBox;	
	fixtureDef.density = 0.3f;
	fixtureDef.friction = 0.3f;
	fixtureDef.isSensor = true;
	body->CreateFixture(&fixtureDef);
}


This code is of HelloWorld.h (Yet to be Renamed :P)
#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"
#import "Controller.h"

// HelloWorld Layer
@interface b2Science : CCLayer {
	b2World* world;
	GLESDebugDraw *m_debugDraw;
	b2MouseJoint *_mouseJoint;
	b2Body* groundBody;
	ControllerClass controller;
}


// returns a Scene that contains the HelloWorld as the only child
+(id) scene;

// adds a new sprite at a given coordinate
-(void) addNewSpriteWithCoords:(CGPoint)p;

@end



Please no suggestions on optimisation or speed of the code, just on the problem at hand. I will get to the other stuff later, I just want it to work for now.

Is This A Good Question/Topic? 0

Replies To: Help with Duplicate Symbol error in C++ / Objective C code

#2 TheStuff  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 16-April 09

Re: Help with Duplicate Symbol error in C++ / Objective C code

Posted 27 March 2010 - 08:08 AM

Please. SOMEONE. I can't figure this out and it's important. It's 4AM and all of my googling skills can not find the reason for this stupid error.

PLEASE.

This post has been edited by TheStuff: 27 March 2010 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Help with Duplicate Symbol error in C++ / Objective C code

Posted 27 March 2010 - 09:54 AM

Some things I would try:

1. inclusion guards
2. making that function static


Based on the error message, the library you're using has a makeController() function as well...?
Was This Post Helpful? 0
  • +
  • -

#4 hlemke  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 18-January 10

Re: Help with Duplicate Symbol error in C++ / Objective C code

Posted 27 March 2010 - 10:39 AM

You are including the .h file twice, once in controller and once in HelloWorld, and it doesn't have header guards. That seems like it would cause a problem like that.

This post has been edited by hlemke: 27 March 2010 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1