Compiling/Linking question

I have installed SDL_image and SDL_TTF in /usr/local (I downloaded the
source tarballs, ran ./configure, make and make install), so the headers
are in /usr/local/include/SDL and the libs are in /usr/local/lib. I
tried to compile my program using the following Makefile:

CC=g++
CFLAGS=-O2 -W -Wall -pedantic sdl-config --cflags
-I//usr/local/include/SDL/
LIBS=-lSDL -lpthread -L/usr/local/lib

all: character.o ally.o enemy.o party.o allyparty.o enemyparty.o draw.o
main

character.o: character.cpp
$(CC) $(CFLAGS) -c character.cpp

ally.o: ally.cpp
$(CC) $(CFLAGS) -c ally.cpp character.o

enemy.o: enemy.cpp
$(CC) $(CFLAGS) -c enemy.cpp character.o

party.o: character.o party.cpp
$(CC) $(CFLAGS) -c party.cpp character.o

allyparty.o: party.o ally.o allyparty.cpp
$(CC) $(CFLAGS) -c allyparty.cpp party.o ally.o

enemyparty.o: party.o enemy.o enemyparty.cpp
$(CC) $(CFLAGS) -c enemyparty.cpp party.o enemy.o

draw.o: draw.cpp allyparty.o enemyparty.o
$(CC) $(CFLAGS) -c draw.cpp enemyparty.o allyparty.o

main: main.cpp allyparty.o enemyparty.o draw.o character.o
$(CC) $(CFLAGS) main.cpp ally.o enemy.o party.o character.o
allyparty.o enemyparty.o draw.o

clean:
rm *.o
rm *~

and when I run make, besides the warnings (which are my problem) I’m
faced with several undefined references:

michael at camille OurRPG $ make
g++ -O2 -W -Wall -pedantic sdl-config --cflags
-I//usr/local/include/SDL/ main.cpp ally.o enemy.o party.o character.o
allyparty.o enemyparty.o draw.o
party.h:7: warning: ‘class Party’ has virtual functions but non-virtual
destructor
allyparty.h:9: warning: ‘class AllyParty’ has virtual functions but
non-virtual destructor
enemyparty.h:8: warning: ‘class EnemyParty’ has virtual functions but
non-virtual destructor
main.cpp:17: warning: unused parameter 'argc’
main.cpp:17: warning: unused parameter 'argv’
main.cpp: In function ‘int main(int, char**)’:
main.cpp:37: warning: ‘x’ may be used uninitialized in this function
draw.o: In function Draw::Init()': draw.cpp:(.text+0xf7): undefined reference toSDL_Init’
draw.cpp:(.text+0x126): undefined reference to SDL_SetVideoMode' draw.cpp:(.text+0x136): undefined reference toTTF_Init’
draw.cpp:(.text+0x157): undefined reference to TTF_OpenFont' draw.cpp:(.text+0x1b0): undefined reference toTTF_RenderText_Shaded’
draw.cpp:(.text+0x1d1): undefined reference to SDL_WM_SetCaption' draw.cpp:(.text+0x1fb): undefined reference toSDL_MapRGB’
draw.cpp:(.text+0x210): undefined reference to SDL_FillRect' draw.cpp:(.text+0x220): undefined reference toSDL_GetError’
draw.cpp:(.text+0x241): undefined reference to SDL_GetError' draw.cpp:(.text+0x262): undefined reference toSDL_GetError’
draw.o: In function Draw::draw(int)': draw.cpp:(.text+0x369): undefined reference toIMG_Load’
draw.cpp:(.text+0x403): undefined reference to SDL_UpperBlit' draw.cpp:(.text+0x453): undefined reference toSDL_UpperBlit’
draw.cpp:(.text+0x466): undefined reference to SDL_Flip' draw.cpp:(.text+0x472): undefined reference toSDL_Delay’
draw.cpp:(.text+0x477): undefined reference to `SDL_Quit’
collect2: ld returned 1 exit status
make: *** [main] Error 1

What am I doing wrong?

After sending my last post, I looked at my main: target in the Makefile
and noticed that I wasn’t adding in $(LIBS). I did that and ran make
again, but the output didn’t change much:

g++ -O2 -W -Wall -pedantic sdl-config --cflags
-I//usr/local/include/SDL/ -lSDL -lpthread -L/usr/local/lib main.cpp
ally.o enemy.o party.o character.o allyparty.o enemyparty.o draw.o
party.h:7: warning: ‘class Party’ has virtual functions but non-virtual
destructor
allyparty.h:9: warning: ‘class AllyParty’ has virtual functions but
non-virtual destructor
enemyparty.h:8: warning: ‘class EnemyParty’ has virtual functions but
non-virtual destructor
main.cpp:17: warning: unused parameter 'argc’
main.cpp:17: warning: unused parameter 'argv’
main.cpp: In function ‘int main(int, char**)’:
main.cpp:37: warning: ‘x’ may be used uninitialized in this function
draw.o: In function Draw::Init()': draw.cpp:(.text+0x136): undefined reference toTTF_Init’
draw.cpp:(.text+0x157): undefined reference to TTF_OpenFont' draw.cpp:(.text+0x1b0): undefined reference toTTF_RenderText_Shaded’
draw.o: In function Draw::draw(int)': draw.cpp:(.text+0x369): undefined reference toIMG_Load’
collect2: ld returned 1 exit status
make: *** [main] Error 1

Here’s draw.h/draw.cpp, which is the only source file that uses any SDL:

michael at camille OurRPG $ cat draw.h
#pragma once

#include <SDL/SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include “allyparty.h”
#include “enemyparty.h”

class Draw
{
public:
Draw(void);
~Draw(void);
void Init();
void draw(int);
int getState();
void setState(int i);
void setAllyParty(AllyParty *p);
void setEnemyParty(EnemyParty *p);
private:
SDL_Surface *allyimage[4], *enemyimage[12];
Uint8 red, green, blue;
SDL_Surface *screen;
int state;
AllyParty *allyparty;
EnemyParty *enemyparty;
Ally *ally[4];
Enemy enemy[12];
TTF_Font
font;

};

michael at camille OurRPG $ cat draw.cpp
#include “draw.h”

Draw::Draw(void)
{

}

Draw::~Draw(void)
{
}

void Draw::Init()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf(“SDL failed to initialize: %s\n”, SDL_GetError());
exit(1);
}

screen = SDL_SetVideoMode(800, 600, 16, SDL_HWSURFACE ||

SDL_DOUBLEBUF);
if (screen == NULL)
{
printf(“SetVideo failed: %s\n”, SDL_GetError());
exit(1);
}

if (!TTF_Init())
{
	printf("TTF failed:  %s\n", SDL_GetError());
	exit(1);
}

font = TTF_OpenFont("ARIAL.TTF", 12); 

SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Color backgroundColor = { 0, 0, 255 };


char buff[100];
for (int i = 0; i < 12; i++)
{
	
	sprintf( buff, "%d", i);

	enemyimage[i] = TTF_RenderText_Shaded(font, buff, foregroundColor,

backgroundColor);

}



	 

SDL_WM_SetCaption("OurRPG", "OurRPG");

red = 0;
green = 0;
blue = 0;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, red, green,

blue));

}
int Draw::getState() { return state; }
void Draw::setState(int i) { state = i; }
void Draw::setAllyParty(AllyParty p)
{
allyparty = p;
Character
temp;
for (int i = 0; i < 4; i++)
{
temp = allyparty->getCharacter(i);
ally[i] = static_cast <Ally*> (temp);
}
}
void Draw::setEnemyParty(EnemyParty p)
{
enemyparty = p;
Character
temp;
for (int i = 0; i < enemyparty->getCount(); i++)
{
temp = enemyparty->getCharacter(i);
enemy[i] = static_cast <Enemy*> (temp);
}
}
void Draw::draw(int state)
{
SDL_Rect allysource[4], allydest[4], enemysource[12], enemydest[12];
for (int i = 0; i < 12; i++)
{
// enemyimage[i] = IMG_Load(enemy[i]->getIMFileName().c_str());
enemysource[i].h = 50;
enemysource[i].w = 50;
enemysource[i].x = 0;
enemysource[i].y = 0;

	enemydest[i].x = enemy[i]->getX();
	enemydest[i].y = enemy[i]->getY();
	enemydest[i].h = 50;
	enemydest[i].w = 50;
}
for (int i = 0; i < 4; i++)
{
	allyimage[i] = IMG_Load(ally[i]->getIMFileName().c_str());


	allysource[i].h = 50;
	allysource[i].w = 50;
	allysource[i].x = 0;
	allysource[i].y = 0;


	allydest[i].x = ally[i]->getX();
	allydest[i].y = ally[i]->getY();
	allydest[i].w = 50;
	allydest[i].h = 50;
	SDL_BlitSurface(allyimage[i], &allysource[i], screen, &allydest[i]);
}
for (int i = 0; i < 12; i++)
{
	SDL_BlitSurface(enemyimage[i], &enemysource[i], screen,

&enemydest[i]);
}

SDL_Flip(screen);
SDL_Delay(5000);

SDL_Quit();

}On Mon, 2009-03-09 at 15:34 -0500, Michael Sullivan wrote:

I have installed SDL_image and SDL_TTF in /usr/local (I downloaded the
source tarballs, ran ./configure, make and make install), so the headers
are in /usr/local/include/SDL and the libs are in /usr/local/lib. I
tried to compile my program using the following Makefile:

CC=g++
CFLAGS=-O2 -W -Wall -pedantic sdl-config --cflags
-I//usr/local/include/SDL/
LIBS=-lSDL -lpthread -L/usr/local/lib

all: character.o ally.o enemy.o party.o allyparty.o enemyparty.o draw.o
main

character.o: character.cpp
$(CC) $(CFLAGS) -c character.cpp

ally.o: ally.cpp
$(CC) $(CFLAGS) -c ally.cpp character.o

enemy.o: enemy.cpp
$(CC) $(CFLAGS) -c enemy.cpp character.o

party.o: character.o party.cpp
$(CC) $(CFLAGS) -c party.cpp character.o

allyparty.o: party.o ally.o allyparty.cpp
$(CC) $(CFLAGS) -c allyparty.cpp party.o ally.o

enemyparty.o: party.o enemy.o enemyparty.cpp
$(CC) $(CFLAGS) -c enemyparty.cpp party.o enemy.o

draw.o: draw.cpp allyparty.o enemyparty.o
$(CC) $(CFLAGS) -c draw.cpp enemyparty.o allyparty.o

main: main.cpp allyparty.o enemyparty.o draw.o character.o
$(CC) $(CFLAGS) main.cpp ally.o enemy.o party.o character.o
allyparty.o enemyparty.o draw.o

clean:
rm *.o
rm *~

and when I run make, besides the warnings (which are my problem) I’m
faced with several undefined references:

michael at camille OurRPG $ make
g++ -O2 -W -Wall -pedantic sdl-config --cflags
-I//usr/local/include/SDL/ main.cpp ally.o enemy.o party.o character.o
allyparty.o enemyparty.o draw.o
party.h:7: warning: ‘class Party’ has virtual functions but non-virtual
destructor
allyparty.h:9: warning: ‘class AllyParty’ has virtual functions but
non-virtual destructor
enemyparty.h:8: warning: ‘class EnemyParty’ has virtual functions but
non-virtual destructor
main.cpp:17: warning: unused parameter 'argc’
main.cpp:17: warning: unused parameter 'argv’
main.cpp: In function ‘int main(int, char**)’:
main.cpp:37: warning: ‘x’ may be used uninitialized in this function
draw.o: In function Draw::Init()': draw.cpp:(.text+0xf7): undefined reference toSDL_Init’
draw.cpp:(.text+0x126): undefined reference to SDL_SetVideoMode' draw.cpp:(.text+0x136): undefined reference toTTF_Init’
draw.cpp:(.text+0x157): undefined reference to TTF_OpenFont' draw.cpp:(.text+0x1b0): undefined reference toTTF_RenderText_Shaded’
draw.cpp:(.text+0x1d1): undefined reference to SDL_WM_SetCaption' draw.cpp:(.text+0x1fb): undefined reference toSDL_MapRGB’
draw.cpp:(.text+0x210): undefined reference to SDL_FillRect' draw.cpp:(.text+0x220): undefined reference toSDL_GetError’
draw.cpp:(.text+0x241): undefined reference to SDL_GetError' draw.cpp:(.text+0x262): undefined reference toSDL_GetError’
draw.o: In function Draw::draw(int)': draw.cpp:(.text+0x369): undefined reference toIMG_Load’
draw.cpp:(.text+0x403): undefined reference to SDL_UpperBlit' draw.cpp:(.text+0x453): undefined reference toSDL_UpperBlit’
draw.cpp:(.text+0x466): undefined reference to SDL_Flip' draw.cpp:(.text+0x472): undefined reference toSDL_Delay’
draw.cpp:(.text+0x477): undefined reference to `SDL_Quit’
collect2: ld returned 1 exit status
make: *** [main] Error 1

What am I doing wrong?

I have installed SDL_image and SDL_TTF in /usr/local (I downloaded the

LIBS=-lSDL -lpthread -L/usr/local/lib

You need -lSDL_ttf and -lSDL_image in there :slight_smile:

-bill!On Mon, Mar 09, 2009 at 03:34:47PM -0500, Michael Sullivan wrote:

And SDLmain? But I didn’t see an error for it…

Jonny DOn Mon, Mar 9, 2009 at 4:48 PM, Bill Kendrick wrote:

On Mon, Mar 09, 2009 at 03:34:47PM -0500, Michael Sullivan wrote:

I have installed SDL_image and SDL_TTF in /usr/local (I downloaded the

LIBS=-lSDL -lpthread -L/usr/local/lib

You need -lSDL_ttf and -lSDL_image in there :slight_smile:

-bill!


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

SDLmain is only required on Windows, I believe.

I have never linked with it under Linux and it never seemed to be required, but using MinGW32 wouldn’t compile without it

Pat________________________________
From: grimfang4@gmail.com (Jonathan Dearborn)
To: A list for developers using the SDL library. (includes SDL-announce)
Sent: Monday, March 9, 2009 9:36:35 PM
Subject: Re: [SDL] Compiling/Linking question

And SDLmain? But I didn’t see an error for it…

Jonny D

On Mon, Mar 9, 2009 at 4:48 PM, Bill Kendrick wrote:

On Mon, Mar 09, 2009 at 03:34:47PM -0500, Michael Sullivan wrote:

I have installed SDL_image and SDL_TTF in /usr/local (I downloaded the

LIBS=-lSDL -lpthread -L/usr/local/lib

You need -lSDL_ttf and -lSDL_image in there :slight_smile:

-bill!


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

Mac OS XOn Mon, Mar 9, 2009 at 11:43 PM, Patryk Bratkowski wrote:

SDLmain is only required on Windows, I believe.


http://codebad.com/