I'm having trouble when compiling my program.
First, here is the makefile I'm using :
LFLAGS = `sdl-config --cflags` `sdl-config --libs` -lSDL -lSDL_image
CFLAGS = -I/usr/include/SDL
HFLAGS = main.h fisherman.h jdv.h
all: jdv
jdv: main.o fisherman.o jdv.o
gcc main.o fisherman.o jdv.o $(LFLAGS) -o jdv -g -Wall
main.o: main.c $(HFLAGS)
gcc -c main.c $(CFLAGS)
fisherman.o: fisherman.c $(HFLAGS)
gcc -c fisherman.c $(CFLAGS)
jdv.o: jdv.c $(HFLAGS)
gcc -c jdv.c $(CFLAGS)
clean:
rm -f *.o
A sample of the program :
in main.h
extern SDL_Surface * event;
extern SDL_Surface * map;
in main.c
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include "main.h"
void loadGame()
{
SDL_Surface * event = NULL;
SDL_Surface * map = NULL;
...
}
And here is the output :
make
gcc main.o fisherman.o jdv.o `sdl-config --cflags` `sdl-config --libs` -lSDL -lSDL_image -lSDL_mixer -o jdv -g -Wall
main.o: in « main »:
main.c:(.text+0x119): undefined reference to « event »
main.c:(.text+0x195): undefined reference to « event »
main.c:(.text+0x1d1): undefined reference to « map »
And it goes for ~ 75 errors like this on different variable. But they are all due to a global variable.
I think it is due to linking problem, but I don't know if it comes from my makefile, or I failed on making a global variable. Why is it telling me the "undefined reference" ? Thanks.
EDIT : I ran sdl-config --libs -> output :
-L/usr/lib/x86_64-linux-gnu -lSDL
In main.h
, you declare event
and map
as global variables.
In main.c
, you define event
and map
as local variables inside loadGame()
. These hide the global variable definitions (and gcc -Wshadow
would tell you about this).
None of the code you show defines the external variables.
Probably, you need to move the two lines shown in loadGame()
outside of the function; that would cure the event
and map
missing references.
The question title mentions SDL_FreeSurface
but the rest of the question doesn't. I'll make a wild guess but maybe you need to link -lSDL_image
before -lSDL
rather than after.