jluebbert
jluebbert

Reputation: 260

Compiling SDL on OS X with makefile

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) $< -o $@

clean:
 rm -rf *.o $(EXECUTABLE)

Upvotes: 2

Views: 5600

Answers (1)

diciu
diciu

Reputation: 29333

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

Upvotes: 3

Related Questions