tags:

views:

603

answers:

1

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)
+2  A: 

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
diciu
Thank you for your response but that doesn't seem to be it. Since I have several classes spread across several .cpp and .h files do I need to do something like this for each one:`g++ `sdl-config --cflags` -c Pieces.cpp`
jluebbert
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
diciu
Alright, I feel like we are getting closer. Now I can compile all of the class files into .o files except for the one the that has #include <SDL/SDL.h>. I get: `IO.cpp: In member function ‘int IO::isKeyDown(int)’: IO.cpp:75: error: ‘SDL_GetKeyState’ was not declared in this scope`
jluebbert
#include <SDL/SDL.h> suggests you're using SDL as a framework, in which case you would need "-framework SDL" - is SDL installed as a framework or as a system library?
diciu
Ahh, it is installed as a framework. Atleast I think it is, it's in /Library/Frameworks.
jluebbert
What does this yield on your machine: "sdl-config --cflags --libs"
diciu
-I/usr/local/include/SDL -D_GNU_SOURCE=1 -D_THREAD_SAFE-L/usr/local/lib -lSDL
jluebbert
Your include statement is inconsistent with what sdl-config says. Try changing #include <SDL/SDL.h> to read #include "SDL.h". As an alternative fix, replace "sdl-config -cflags -libs" with "-framework SDL".
diciu
Then I receive:In file included from Game.h:4, from main.cpp:3:IO.h:4:17: error: SDL.h: No such file or directory
jluebbert
Well, I took out the function that had a call to `SDL_GetKeyState(NULL)` and everything compiled and worked using the commands above. Any ideas?
jluebbert