Reputation: 562
I'm learning my way around SDL and everything's been working fine up until now. Whenever I try to compile my code, I get the following error:
Undefined symbols for architecture x86_64:
"Tile::draw(SDL_Surface*)", referenced from:
_SDL_main in ccTWWnIW.o
"Tile::update()", referencedfrom:
_SDL_main in ccTWWnIW.o
"Tile::Tile(int)", referenced from:
_SDL_main in ccTWWnIW.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
What's strange about it is that it only occurs when I include the "level.h" header in my "player.cpp" bit of code.. while including "level.h" in my main program doesn't set off the compiler at all. I make use of the class defined in "level.h" in both the main program - to instantiate the tiles, update them and blit them to the screen - and in "player.cpp" - to check for collisions. I commented out all the parts of "player.cpp" that made use of any components defined in "level.h" and I still got the compiler errors. I've included the same SDL headers as in "player.cpp" and have set the flags accordingly on my compiler so I really don't see why the "ld: symbol(s) not found for architecture x86_64" message is cropping up.
The bits of "level.cpp" that are referenced in the error message:
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include "Variables.h"
#include <cstdlib>
#include "level.h"
class Tile{
public:
int damage;
int velocity;
bool solid;
SDL_Rect position;
SDL_Rect crop;
SDL_Surface* image;
Tile(int type);
~Tile();
bool update();
void draw(SDL_Surface* target);
};
...
Tile::Tile(int type){
if(type == 0){
damage = 0;
velocity = 20;
solid = true;
position.x = 600;
position.y = rand() % 500;
SDL_Surface* temp_image = IMG_Load_RW(SDL_RWFromFile("spritesheet.png", "rb"), 1);
image = SDL_DisplayFormatAlpha(temp_image);
SDL_FreeSurface(temp_image);
crop.x = 0;
crop.y = 0;
crop.w = 50;
crop.h = 50;
}
}
...
bool Tile::update(){
position.x = position.x - velocity;
if(position.x <= 0) {
position.x = 700;
position.y = rand()%500;
}
else return 0;
}
...
void Tile::draw(SDL_Surface* target){
SDL_BlitSurface(image, &crop, target, &position);
}
I know that the coding style is rubbish, this is only me messing about and learning SDL.
Upvotes: 0
Views: 194
Reputation: 166
I'm not sure what the exact issue is based on this, but there are a few things you should do.
Put header guards around your headers, eg. in level.h, you would want something like:
#ifndef LEVEL_H_
#define LEVEL_H_
at the top, and:
#endif
at the bottom. This is to prevent symbols from being defined multiple times when the file is included by several different source files.
Second, you need to put class definitions into header (.h) files not .cpp files. It would probably be much nicer in your case to move the tile stuff into its own files (tile.h, tile.cpp) but either way the tile class definition needs to be in a header file.
Upvotes: 1