I've read a number of posts on this issue but I haven't found anything particularly helpful. I am trying to make a weldJoint when two sprites meet using a contact listener. I keep getting the following error:
Apple Mach-O Linker (Id) Error
"_touchingBodies", referenced from:
SubcContactListener::BeginContact(b2Contact*) in SubcContactListener.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is my contact listener.
SubcContactListener.h:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import <vector>
typedef std::pair<b2Body*, b2Body*> bodyPair;
typedef std::vector<bodyPair> thingsThatTouched;
extern thingsThatTouched touchingBodies;
class SubcContactListener : public b2ContactListener {
public:
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
};
SubcContactListener.mm:
#import "SubcContactListener.h"
void SubcContactListener:: BeginContact(b2Contact *contact) {
touchingBodies.push_back( std::make_pair(contact->GetFixtureA()->GetBody(), contact->GetFixtureB()->GetBody()) );
}
I added:
thingsThatTouched touchingBodies;
to the HelloWorldLayer.h interface.
Finally, in the tick method of the HelloWorldLayer.mm (after the timestep):
b2WeldJointDef weldJointDef;
b2WeldJoint *weldJoint;
for (int i = 0; i < touchingBodies.size(); i++) {
b2Body* bodyA = touchingBodies[i].first;
b2Body* bodyB = touchingBodies[i].second;
weldJointDef.Initialize(bodyA, bodyB, bodyA->GetWorldCenter());
weldJointDef.collideConnected = false;
weldJoint = (b2WeldJoint*)world->CreateJoint(&weldJointDef);
}
touchingBodies.clear();
Please help, I've been at it for a while.
extern thingsThatTouched touchingBodies;
Such extern variables must be defined as static C variables elsewhere, not instance variables.
In light of a better design, I recommend to scrap the extern variable and instead access the touchingBodies through the HelloWorldLayer by adding a Singleton interface to it.
You'd then be able to access it from anywhere:
[HelloWorldLayer sharedWorldLayer].touchingBodies;
Related
I have added a c++ library .dylib to my objective c cocoa os x app. When I am trying call a function that I wrote into .dylib with my .header class I can't compile because I get the errors. In c+++ project all is correct, but into objective c no.
My viewController:
#import "myHeader.mm"
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
mean(5.8, 5.8);
}
My class header:
#ifdef __cplusplus
extern "C" {
#endif
//void donothing(void);
//double mean(double x, double y);
double mean(double x, double y);
#ifdef __cplusplus
}
#endif
The errors are:
Undefined symbols for architecture x86_64:
"_mean", referenced from:
-[ViewController viewDidLoad] in ViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How can solve this? What is the correct form to add .dylib to my project?
Thanks!
I'm trying to get Box2D running with my Cocos2d-x project. I'm adding collision detection but I get a linker error
Undefined symbols for architecture i386: "CContactListener::CContactListener()", referenced from: HelloWorld::init() in HelloWorldScene.o
I've tried several things and researched for a few days but can't figure it out. Any help wold be great.
Here's some code pieces
HelloWorldScene.h
CContactListener *_contactListener; //Variable declared and #include "ContactListener.h" is present at the top
HelloWorldScene.cpp
_contactListener = new CContactListener(); //This line gets the error
_world->SetContactListener(_contactListener);
ContactListener.h
class CContactListener : public b2ContactListener {
public:
CContactListener();
~CContactListener();
std::vector<ContactData>_contacts;
virtual void BeginContact(b2Contact* contact);
virtual void EndContact(b2Contact* contact);
virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
};
ContactListener.cpp
#include "ContactListener.h"
CContactListener::CContactListener(): _contacts()
{
}
CContactListener::~CContactListener()
{
}
//...other functions
CContactListener.cpp was not added the the target build in Xcode. I simply checked the target box for my project in the File Inspector for the .ccp file. Easy fix.
I am trying to create a very classic singleton on C++, Mac OS using Xcode
I create the Class MySingleton as follow:
class MySingleton{
private:
int val;
static MySingleton *instance;
MySingleton(){
val = 0;
}
public:
int getVal(){
return val;
}
void setVal(int iVal){
val = iVal;
}
static MySingleton* getInstance(){
if(instance == NULL)
instance = new MySingleton();
return instance;
}
};
Linker is complaining about static MySingleton* getInstance()
Follow the Linker message:
Undefined symbols for architecture x86_64: "MySingleton::instance",
referenced from:
MySingleton::getInstance() in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
Could somebody help on this? Perhaps I need to set something on Xcode, which by the way is version 4.2.1, and I am not able in doing this.
Thanks.
You need to define static variable in your cpp file, like this:
MySingleton *MySingleton::instance = 0;
The static member MySingleton needs to be defined in the cpp file. In the header you have have only declared it. See this for more information: Why do static variables need to be declared twice in C++
As far as I can see, you've declared static MySingleton *instance but haven't defined it anywhere.
I am continuously getting that error, and it's driving me crazy!
Undefined symbols for architecture x86_64:
"SSResourcesDepot::_sharedInstance", referenced from:
SSResourcesDepot::sharedInstance() in SSResourcesDepot.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My code looks like:
#ifndef SolarSystem_SSResourcesDepot_h
#define SolarSystem_SSResourcesDepot_h
#include "SSResource.h"
/* SSResourcesDepot is implemented as a Singleton Depot that keeps track of all
* requested Resource objects, and avoid loading them twice in memory. */
class SSResourcesDepot {
SSResourcesDepot() {};
SSResourcesDepot(SSResourcesDepot const&){};
SSResourcesDepot& operator=(SSResourcesDepot const&){};
static SSResourcesDepot* _sharedInstance;
SSResource* _search(std::string resourceName);
SSResource* _load(std::string resourceName);
public:
static SSResourcesDepot* sharedInstance();
SSResource* requestResource(std::string resourceName);
};
#endif
and:
#include <iostream>
#include "SSResourcesDepot.h"
#pragma mark Public methods
SSResourcesDepot* SSResourcesDepot::sharedInstance() {
if (SSResourcesDepot::_sharedInstance == 0) {
SSResourcesDepot::_sharedInstance = new SSResourcesDepot();
}
return SSResourcesDepot::_sharedInstance;
}
SSResource* SSResourcesDepot::requestResource(std::string resourceName) {
SSResource *resource = this->_search(resourceName);
if (resource == NULL) resource = this->_load(resourceName);
return resource;
}
#pragma mark Private methods
SSResource* SSResourcesDepot::_search(std::string resourceName) {
return NULL;
}
SSResource* SSResourcesDepot::_load(std::string resourceName) {
return NULL;
}
It seems completely functional to me, but Apple-O-Matcher keeps complaining, and it doesn't let me compile ... :-S
Thanks in advance!
You didn't initialize your static member.
Add
SSResourcesDepot* SSResourcesDepot::_sharedInstance = NULL;
to your implementation file.
I'm trying to make a Singleton class in C++ with Xcode. It's a really basic class and I get a linker error that I don't understand. Can any1 help please?
Here is the class header file :
#ifndef _NETWORK_H_
#define _NETWORK_H_
#include <iostream>
#include <list>
#include "Module.h"
using namespace std;
/*
* Assume only one network can run at a time
* in the program. So make the class a singleton.
*/
class Network {
private:
static Network* _instance;
list<Module*> _network;
public:
static Network* instance();
};
#endif
Here is the impl file :
#include "Network.h"
Network* Network::instance() {
if (!_instance)
_instance = new Network();
return _instance;
}
Here is the compiler error :
Undefined symbols for architecture x86_64:
"Network::_instance", referenced from:
Network::instance() in Network.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You need to declare actual storage for Network::_instance somewhere. Likely the impl. file.
Try adding to your impl file:
Network *Network::_instance=0;
You need to define your instance in the implementation file:
#include "Network.h"
Network *Network::_instance;
The static Network *_instance; declaration just says that there exists a Network::_instance somewhere. You must provide a single definition somewhere for it to actually exist.