10

I'm getting started with SDL and C programming. I have experience with other programming languages, but linking/compiling libraries in C is new to me. I am using Mac 10.8 and have installed latest stable 2.0 using the instructions in the read me (./configure; make; make install). Here is the sample code that I am trying to compile:

#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"

int main(void)
{
  if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
    fprintf(stderr, "\nUnable to initialize SDL:  %s\n", SDL_GetError());
    return 1;
  }
  atexit(SDL_Quit);

  return 0;
}

When I try to compile my script using gcc example.c, I get an error:

example.c:3:17: error: SDL.h: No such file or directory
example.c: In function ‘main’:
example.c:7: error: ‘SDL_INIT_VIDEO’ undeclared (first use in this function)
example.c:7: error: (Each undeclared identifier is reported only once
example.c:7: error: for each function it appears in.)
example.c:7: error: ‘SDL_INIT_TIMER’ undeclared (first use in this function)
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:11: error: ‘SDL_Quit’ undeclared (first use in this function)

I tried searching the wiki, and tutorials, and any kind of documentation that I could find, but I could not find any example anywhere that showed how to properly compile a C program that uses SDL.

What do I need to do to compile this program?

Andrew
  • 227,796
  • 193
  • 515
  • 708

3 Answers3

13

I found out you can use a tool called pkg-config to find out the compiler flags expected for a specific library.

$ pkg-config --cflags --libs sdl2
-D_THREAD_SAFE -I/usr/local/include/SDL2 -I/usr/X11R6/include -L/usr/local/lib -lSDL2

$ gcc example.c $(pkg-config --cflags --libs sdl2)

If you are using a Makefile, you need to prefix the command with shell:

all:
    gcc example.c $(shell pkg-config --cflags --libs sdl2)
Andrew
  • 227,796
  • 193
  • 515
  • 708
13

A general hint for C beginners: read error logs top-down: often fixing first error will resolve all other. In your case first error is:

example.c:3:17: error: SDL.h: No such file or directory

As others have said, you need to instruct gcc where to find SDL.h. You can do this by providing -I option.

To check where SDL.h is installed by default I would issue

./configure --help

in the directory where you did build libsdl. Then look for --prefix, under Linux default prefix is often /usr/local. To compile your example I would issue (on Linux):

gcc example.c -I/usr/local/include

But the above command compiles and links the code. After successful compilation, gcc would throw another bunch of errors, one of them being undefined reference.

To prevent that, full command line to build your example (on Linux at least) would be:

gcc example.c -I/usr/local/include -L/usr/local/lib -lSDL

Where:

  • -I points compiler to directory with SDL.h,
  • -L points linker to directory with libSDL.a (or libSDL.so),
  • -l instructs linker to link with library, in our case libSDL.a or libSDL.so. Note that the lib prefix and .a/.so suffix is missing.

Please note that I didn't check this instruction, even on Linux machine (on the other hand I have no access to Mac OS machine).

One more thing: by default binary with the compiled and linked example will be called a.out. To change that you can provide -o option to gcc.

smbear
  • 1,007
  • 9
  • 17
0

tl;dr

sudo apt install libsdl1.2-dev

You are missing the SDL library files. Just instal them and everything should work out of the box.

Different versions ans extensions

There are multiple versions of SDL, make sure you install the one that is required by your application. There are also additional libraries (called projects) for SDL that you also need to install if you use their features. For example if you use TTF fonts or image related functionalities. Just press TAB twice after you typed in sudo apt install libsdl to see all the available packages.

totymedli
  • 29,531
  • 22
  • 131
  • 165