使用 makefile 在 OS X 上编译 SDL

发布于 2024-08-13 14:01:08 字数 1158 浏览 3 评论 0原文

我正在尝试编译我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-08-20 14:01:08

我认为你的编译问题与SDL main函数有关。编译

失败是因为您缺少对“Game.o”的引用或调用编译 Game.cpp 所产生的任何对象文件。尝试:

g++ -o tetris main.cpp Game.o Pieces.o Whateverelse.o `sdl-config --cflags --libs` -framework Cocoa

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:

g++ -o tetris main.cpp Game.o Pieces.o Whateverelse.o `sdl-config --cflags --libs` -framework Cocoa
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文