使用 makefile 在 OS X 上编译 SDL
我正在尝试编译我在 OS X 上用 C++ 和 SDL 编写的俄罗斯方块程序。首先我尝试这样做:
`g++ -o tetris main.cpp `sdl-config --cflags --libs` -framework Cocoa`
并得到这个:
Undefined symbols:
"Game::startGame()", referenced from:
_main in ccQMhbGx.o
"Game::Game()", referenced from:
_main in ccQMhbGx.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
这是 main.cpp 文件:
#include <iostream>
#include "Game.h"
int main(int argc, char* argv[]) {
Game *game = new Game();
game->startGame();
return 0;
}
Game.h
是游戏包含所有其他类(Board.h、IO.h、Piece.h、Pieces.h)的类,并且包含游戏的主要逻辑。
我真的很希望能够为此编写一个 makefile 或找到某种方法来轻松地将其分发给朋友。
编辑:
这是最终的 makefile,以防其他人遇到同样的问题:
CC=g++
CFLAGS=-c -Wall
SDLFLAGS=`sdl-config --cflags --libs` -framework Cocoa
SOURCES=main.cpp Game.cpp IO.cpp Board.cpp Pieces.cpp Piece.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=tetris
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) $(SDLFLAGS) -o $@
.cpp.o:
$(CC) $(CFLAGS) $< -o $@
clean:
rm -rf *.o $(EXECUTABLE)
I'm trying to compile the tetris program I wrote with C++ and SDL on OS X. First I tried doing this:
`g++ -o tetris main.cpp `sdl-config --cflags --libs` -framework Cocoa`
and got this:
Undefined symbols:
"Game::startGame()", referenced from:
_main in ccQMhbGx.o
"Game::Game()", referenced from:
_main in ccQMhbGx.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Here is the main.cpp file:
#include <iostream>
#include "Game.h"
int main(int argc, char* argv[]) {
Game *game = new Game();
game->startGame();
return 0;
}
Game.h
is the game class where all of the other classes (Board.h, IO.h, Piece.h, Pieces.h) are included and the main logic of the game is contained.
I'd really like to be able to write a makefile for this or find some way to easily distribute it to friends.
EDIT:
here is the final makefile in case anyone else is having the same problem:
CC=g++
CFLAGS=-c -Wall
SDLFLAGS=`sdl-config --cflags --libs` -framework Cocoa
SOURCES=main.cpp Game.cpp IO.cpp Board.cpp Pieces.cpp Piece.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=tetris
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) $(SDLFLAGS) -o $@
.cpp.o:
$(CC) $(CFLAGS) lt; -o $@
clean:
rm -rf *.o $(EXECUTABLE)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的编译问题与SDL main函数有关。编译失败是因为您缺少对“Game.o”的引用或调用编译 Game.cpp 所产生的任何对象文件。尝试:
I think your compile issue is related to the SDL main function.The compile failure is because you're missing references to "Game.o" or whatever the object file resulted out of compiling Game.cpp is called. Try: