[SDL] OT - Undefined references in my project

Ulrich Eckhardt doomster at knuut.de
Sat Dec 1 07:17:44 PST 2007


On Saturday 01 December 2007 13:24:44 Michael Sullivan wrote:
> CC=g++

No. CC is the C compiler, for C++ use CXX. Also, don't define those, 'make' 
knows defaults for them. Instead, if you need to override them, you can 
invoke make with

  CXX=g++-3.4 make

> CFLAGS=-W -Wall -pedantic

Dito, only that it's traditionally CXXFLAGS. Also, I'd rather not set them in 
the makefile. Neither of those make a difference though.

> LIBS=`sdl-config --cflags --libs` -lSDL_image -lSDL_gfx -lSDL_ttf

Okay, this one is definitely wrong: you want the output 
of 'sdl-config --cflags' in the CXXFLAGS, because if you put them into the 
LIBS var they simply don't work and also make no sense there. Note: what you 
need to set in the makefile is that part, therefore I'd do

  SDL_CFLAGS=`sdl-config --cflags`
  SDL_LIBS=`sdl-config --cflags` -lSDL_image -lSDL_gfx -lSDL_ttf

and then refer to those plus the user-overridable CFLAGS when compiling.

> character.o: character.cpp
> 	reset; $(CC) $(CFLAGS) -c character.cpp;rm *~

Just for your info: in order to get character.o, you neither need to 
invoke 'reset' (at least I think so) nor do you need to remove backup files 
(*~), so this only makes it harder for us to understand. Define separate 
targets for those. Further, you can use several lines for the target, also 
making it a bit easier to read. Lastly, if you change character.h, this will 
not(!) cause character.cpp to be recompiled like this! I'd suggest that you 
do a 

  HEADERS=battle.h character.h

and then define every target like

  battle.o: battle.cpp $(HEADERS)

Just to be on the safe side. Later, you can learn about determining the 
dependencies automatically to remove unnecessary ones. ;)

> battle.o: battle.cpp
> 	reset; $(CC) $(CFLAGS) -c battle.cpp -I/usr/include/SDL/; rm *~

You can remove the -I, if you put the output of 'sdl-config --cflags' into 
CFLAGS it will work without it. ;)

> all: battle.o character.o
> 	reset; $(CC) $(CFLAGS) battle.o character.o $(LIBS) -o battle; rm *~
>
> character.o and battle.o are created, but when I try to make all, I get
> the following errors:
>
> battle.o: In function `battle::battle()':
> battle.cpp:(.text+0x9e3): undefined reference to
> `Character::Character()'
[...]
> I don't understand why.  This is my first SDL project, and my first
> multifile C/C++ project, so I'm kinda out of my element here.

Maybe the above suggestions (in particular the missing header file dependency) 
already help. Otherwise, you should first try to compile a simple multi-file 
C++ project without SDL. Try to compile e.g. something like

  g++ -W -Wextra file1.cpp file2.cpp -o example

If that doesn't work, you did something wrong splitting the single file into 
multiple files. In any case, it has nothing to do with SDL then.

good luck

Uli


More information about the SDL mailing list