OT - Undefined references in my project

I’m sorry to ask this here, but I’ve tried a few other lists, and
they’ve given me a few suggestions (which didn’t work), so I thought I’d
try here. I suspect this has something to do with my Makefile. This is
the first project I’ve done when I’ve attempted to use a Makefile. My
full code is too long to politely past into an email, but I’ve posted it
at http://www.espersunited.com/~michael/needhelp.txt

Here’s the Makefile:

CC=g++
CFLAGS=-W -Wall -pedantic
LIBS=sdl-config --cflags --libs -lSDL_image -lSDL_gfx -lSDL_ttf

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

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

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 toCharacter::Character()'
battle.cpp:(.text+0xb35): undefined reference to
Character::~Character()' battle.cpp:(.text+0xb83): undefined reference toCharacter::~Character()'
battle.cpp:(.text+0xbd1): undefined reference to
Character::~Character()' battle.cpp:(.text+0xc16): undefined reference toCharacter::~Character()'
battle.o: In function battle::battle()': battle.cpp:(.text+0xf19): undefined reference toCharacter::Character()'
battle.cpp:(.text+0x106b): undefined reference to
Character::~Character()' battle.cpp:(.text+0x10b9): undefined reference toCharacter::~Character()'
battle.cpp:(.text+0x1107): undefined reference to
Character::~Character()' battle.cpp:(.text+0x114c): undefined reference toCharacter::~Character()'
character.o: In function Character::Character(char*, long, long)': character.cpp:(.text+0xdb): undefined reference tovtable for
Character’
character.o: In function Character::Character(char*, long, long)': character.cpp:(.text+0x12d): undefined reference tovtable for
Character’
collect2: ld returned 1 exit status

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. Thank you
in advance for your help…
-Michael Sullivan-

Michael Sullivan schrieb:

all: battle.o character.o
reset; $(CC) $(CFLAGS) battle.o character.o $(LIBS) -o battle; rm *~

That should read

battle: battle.o character.o

Although it doesn’t have anyting to do with your problem. Also include
as the first two lines

.PHONY: all
all: battle

battle.cpp:(.text+0x9e3): undefined reference to
Character::Character()' battle.cpp:(.text+0xb35): undefined reference toCharacter::~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. Thank you
in advance for your help…

Do you call the constructor anywhere, i.e. do you have a declaration
like any of the following

Character c();
Character c = Character();
Character *c = new Character();

Maybe you use a class which relies on the empty constructor and
assignment-operator, like std::map?

Character c(‘a’);
std::map<int, Character> mymap;
mymap[7] = c;

Note that the above example invokes the Character() constructor as
internally the “new” variant is called.

Simply create an empty () constructor and destructor and you’ll likely
be fine.

Greetings,
Johannes

Michael Sullivan schrieb:

all: battle.o character.o
reset; $(CC) $(CFLAGS) battle.o character.o $(LIBS) -o battle; rm *~

That should read

battle: battle.o character.o

Although it doesn’t have anyting to do with your problem. Also include
as the first two lines

.PHONY: all
all: battle

battle.cpp:(.text+0x9e3): undefined reference to
Character::Character()' battle.cpp:(.text+0xb35): undefined reference toCharacter::~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. Thank you
in advance for your help…

Do you call the constructor anywhere, i.e. do you have a declaration
like any of the following

Character c();
Character c = Character();
Character *c = new Character();

Maybe you use a class which relies on the empty constructor and
assignment-operator, like std::map?

Character c(‘a’);
std::map<int, Character> mymap;
mymap[7] = c;

Note that the above example invokes the Character() constructor as
internally the “new” variant is called.

Simply create an empty () constructor and destructor and you’ll likely
be fine.

michael at camille ourrpg $ grep ‘Character()’ character.h
Character();
virtual ~Character();

Greetings,
Johannes

Also, my Makefile has changed.

CXX=g++
CFLAGS=-W -Wall -pedantic
LIBS=sdl-config --cflags --libs -lSDL_image -lSDL_gfx -lSDL_ttf
INC = -I/usr/include/SDL

all: battle

battle: battle.o character.o
$(CXX) -o $@ battle.o character.o $(LIBS)

%.o : %.cpp
$(CXX) $(CFLAGS) -c $< -o $@ $(INC)On Sat, 2007-12-01 at 15:42 +0100, Johannes Bauer wrote:

The makefile in your text file is different from the one in your email, so
I’ll go by the text file:

michael at camille ourrpg $ nl Makefile
1 CC=g++
2 CFLAGS=-W -Wall -pedantic
3 LIBS=sdl-config --cflags --libs -lSDL_image -lSDL_gfx -lSDL_ttf
4
5 character.o: character.cpp
6 reset; $(CC) $(CFLAGS) -c character.cpp;rm *~
7
8 battle.o: battle.cpp
9 reset; $(CC) $(CFLAGS) -c battle.cpp character.o $(LIBS); rm *~
10
11 all: battle.o character.o
12 reset; $(CC) $(CFLAGS) battle.o character.o $(LIBS) -o
battle; rm *~

It says here you’re outputting the compiled battle.cpp as character.o. This
will cause all kinds of problems for you.On Dec 1, 2007 7:24 AM, Michael Sullivan wrote:

I’m sorry to ask this here, but I’ve tried a few other lists, and
they’ve given me a few suggestions (which didn’t work), so I thought I’d
try here. I suspect this has something to do with my Makefile. This is
the first project I’ve done when I’ve attempted to use a Makefile. My
full code is too long to politely past into an email, but I’ve posted it
at http://www.espersunited.com/~michael/needhelp.txthttp://www.espersunited.com/~michael/needhelp.txt

Here’s the Makefile:

CC=g++
CFLAGS=-W -Wall -pedantic
LIBS=sdl-config --cflags --libs -lSDL_image -lSDL_gfx -lSDL_ttf

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

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

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 toCharacter::Character()'
battle.cpp:(.text+0xb35): undefined reference to
Character::~Character()' battle.cpp:(.text+0xb83): undefined reference toCharacter::~Character()'
battle.cpp:(.text+0xbd1): undefined reference to
Character::~Character()' battle.cpp:(.text+0xc16): undefined reference toCharacter::~Character()'
battle.o: In function battle::battle()': battle.cpp:(.text+0xf19): undefined reference toCharacter::Character()'
battle.cpp:(.text+0x106b): undefined reference to
Character::~Character()' battle.cpp:(.text+0x10b9): undefined reference toCharacter::~Character()'
battle.cpp:(.text+0x1107): undefined reference to
Character::~Character()' battle.cpp:(.text+0x114c): undefined reference toCharacter::~Character()'
character.o: In function Character::Character(char*, long, long)': character.cpp:(.text+0xdb): undefined reference tovtable for
Character’
character.o: In function Character::Character(char*, long, long)': character.cpp:(.text+0x12d): undefined reference tovtable for
Character’
collect2: ld returned 1 exit status

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. Thank you
in advance for your help…
-Michael Sullivan-


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Cheers,
Josh

PGP: http://revvy.box43.net/Josh_Matthews.asc

The makefile in your text file is different from the one in your
email, so I’ll go by the text file:

I’ve updated the text file.On Sat, 2007-12-01 at 09:53 -0500, Josh Matthews 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. :wink:

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. :wink:

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 toCharacter::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

UliOn Saturday 01 December 2007 13:24:44 Michael Sullivan wrote:

Michael Sullivan schrieb:

michael at camille ourrpg $ grep ‘Character()’ character.h
Character();
virtual ~Character();

That’s a declaration, no definition.

A definition would be

Character() { };
virtual ~Character() { };

Greetings,
Johannes