Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile an example SDL program written in C?

Tags:

c

sdl

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?

like image 691
Andrew Avatar asked Oct 14 '13 17:10

Andrew


1 Answers

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.

like image 192
smbear Avatar answered Sep 25 '22 22:09

smbear