I'm currently doing some C programming and I actually want to use the SDL library. I want to build a Small 2D game in C on Linux to sharp my skills a bit.
My issue is I'm not a super Makefile user nor library on Linux super user, I just configure things once when on a project and that's it.
So I have some trouble compiling SDL2 programs on UBUNTU 14.04.
I downloaded the latest SDL library from : http://www.libsdl.org/download-2.0.php
Then I installed it with the default step:
./configure
make
sudo make install
After that I can see that there is something in /usr/include/SDL2 so I guess it is installed.
#include <stdlib.h>
#include <stdio.h>
#include <SDL2/SDL.h>
int main(int argc, char *argv[])
{
printf(“SDL test\n”);
return 0;
}
Because I'm still learning Makefiles and SDL I didn't figure it out to make it.
but I found this Makefile to compile the old SDL not the SDL2
CPP=gcc
CFLAGS=-O3
LDFLAGS=-lSDL -lSDL_mixer #Linker
EXEC=test
all: ${EXEC}
${EXEC}: ${EXEC}.o
${CPP} $(CFLAGS) -o ${EXEC} ${EXEC}.o ${LDFLAGS}
${EXEC}.o: ${EXEC}.c
${CPP} $(CFLAGS) -o ${EXEC}.o -c ${EXEC}.c
clean:
rm -fr *.o
mrproper: clean
rm -fr ${EXEC}
But this Makefile is not working for me it says that it doesn't know the lSDL_Mixer and other stuff.
How can I build a workable Makefile to compile C program with SDL2 using Makefiles and vim editor.
Thanks in advance for your help
I'll go ahead and clean up that Makefile for you.
CFLAGS = -O3
LDFLAGS =
appname = test
all: $(appname)
clean:
rm -f $(appname) *.o
.PHONY: all clean
sdl_cflags := $(shell pkg-config --cflags sdl2 SDL2_mixer)
sdl_libs := $(shell pkg-config --libs sdl2 SDL2_mixer)
override CFLAGS += $(sdl_cflags)
override LIBS += $(sdl_libs)
$(appname): test.o
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
Variables in a makefile should be used with $(...)
, not ${...}
.
pkg-config
has an entry for SDL_Mixer, sdl-config
does not. pkg-config
is much more general.
using override
with the SDL flags allows you to run something like make CFLAGS="-O0 -g"
without breaking SDL support.
This is actually important: the SDL library flags have to be at the end of the command line, in LIBS
, due to the fact that the GNU linker is sensitive to the order in which libraries are specified.
You don't need explicit rules for .c
files, because GNU Make has implicit rules which are just fine.
Note that there are some very important features missing from this makefile. For example, it won't automatically recompile things when you change your header files. I would recommend that you use another build system, such as CMake or SCons, which handles this automatically. You can also do it with Make, but you'd have to paste several arcane lines of code into your makefile.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments