How to install/compile SDL2 C code on Linux/Ubuntu

Raphael Teubner

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

Dietrich Epp

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)

Explanation

  • 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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to install/compile SDL2 C code on Linux/Ubuntu

From Dev

How to compile a C++ program with SDL2 on OS X?

From Dev

Makefile C with SDL2

From Dev

getting segmentation fault (core dumped) with my sdl2 opengl code in c

From Dev

Drawing Line is flickering in SDL2 C++. How To Modify My Game Loop?

From Dev

SDL2 C++ Taking a screenshot

From Dev

SDL2 C++ Taking a screenshot

From Dev

SDL2 C++ Unhandled Exception

From Dev

How to render text in SDL2?

From Dev

SDL2: How to properly toggle fullscreen?

From Dev

SDL2 How to draw dotted line

From Dev

How to draw rotated polygons in SDL2

From Dev

How to rotate a texture in SDL2?

From Dev

How to use SDL2 and SDL_image with cmake

From Dev

How to use SDL2 and SDL_image with cmake

From Dev

Undefined reference to SDL2 functions in linked library (Code::Blocks)

From Dev

My code won't display sprite SDL2

From Dev

ImGui, SDL2 & code block, struggling to setup

From Dev

C++ Access violation when using SDL_ttf with SDL and SDL2

From Dev

A method to rotate a texture in SDL2, C++

From Dev

C++ SDL2 Get Mouse Coordinates Without Delay

From Dev

C : Unix SDL2 library : undefined reference, issue in the Makefile?

From Dev

Creating a Window with a Rectangle in it with C++ and SDL2

From Dev

Am i deleting sdl2 and opengl right c++

From Dev

C++ / SDL2 - Ball bouncing/glitching together

From Dev

SDL2 & C++ - Load an image as a pixel array

From Dev

C++ & SDL2 - "Auto-Generate" used DLLs?

From Dev

C++ SDL2 Programm crashes on switch loop

From Dev

C++ SDL2, moving too fast