我正在学习如何使用 SDL,到目前为止一切正常。每当我尝试编译我的代码时,我都会收到以下错误:
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
奇怪的是,它仅在我的“player.cpp”代码位中包含“level.h”头文件时发生。而在我的主程序中包含“level.h”并不会触发编译器全部。我在两个主程序中使用“level.h”中定义的类——实例化图块,更新它们并将它们blit到屏幕上——以及在“player.cpp”中——来检查碰撞。我注释掉了使用“level.h”中定义的任何组件的“player.cpp”的所有部分,但我仍然遇到编译器错误。我在“player.cpp”中包含了相同的 SDL 标头,并在我的编译器上相应地设置了标志,所以我真的不明白为什么会出现“ld: symbol(s) not found for architecture x86_64”消息.
错误消息中引用的“level.cpp”位:
#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);
}
我知道编码风格很垃圾,这只是我在玩弄和学习 SDL。