SDL problem...

closed account (y8h7M4Gy)
Hello. I've just installed SDL for C++, but I'm getting an error saying that the file is not found...

root@edward:~# g++ -Wall test.cpp -o test
test.cpp:1:49: error: SDL.h: No such file or directory

Do I need to put the SDL.h file into the same directory as my file or did I not install SDL properly?

Thanks.
You just need to specify an include path to look for the header(s).

Start by finding where SDL.h is located. I would guess it's somewhere in /usr/include, /usr/local, etc. Try:
> find /usr -name SDL.h

Once you know the directory that contains it, add -I[PATH] argument to g++, where [PATH] is replaced by the directory.

Here's a made-up example:
> find /usr -name SDL.h
/usr/include/asdf/SDL.h
...
> g++ -Wall -I/usr/include/asdf test.cpp -o test
...


I'm not familiar with SDL, so you may also need to include the library. You'll know this if you start getting undefined reference errors. Then once compiled, the LD_LIBRARY_PATH environment variable can be used to specify the path to the library for runtime.

man g++ for more information about GCC parameters. Note: -I, -L, and -l.
Last edited on
closed account (y8h7M4Gy)
Ok, it works. but now there is another error...

/tmp/ccMnkG2I.o: In function `main':
test.cpp:(.text+0x1f): undefined reference to `SDL_Init'
test.cpp:(.text+0x2e): undefined reference to `SDL_GetError'
test.cpp:(.text+0x63): undefined reference to `SDL_Quit'
collect2: ld returned 1 exit status


Here is my C++ code ( I just got it from an example )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "SDL.h"   /* All SDL App's need this */
#include <stdio.h>

int main(int argc, char *argv[]) {
    
    printf("Initializing SDL.\n");
    
    /* Initialize defaults, Video and Audio */
    if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { 
        printf("Could not initialize SDL: %s.\n", SDL_GetError());
        exit(-1);
    }

    printf("SDL initialized.\n");

    printf("Quiting SDL.\n");
    
    /* Shutdown all subsystems */
    SDL_Quit();
    
    printf("Quiting....\n");

    exit(0);
}
Last edited on
Those functions are in libSDL.so, according to:
http://sdl.beuc.net/sdl.wiki/SDL_Init

So, you'll need to find its location just as before and then add a -L[PATH] and a -lSDL to the g++ command so that the linker finds the shared object.

Then to execute the program, you'll need to let the shell know where to find the shared object file. To do that set the environment variable LD_LIBRARY_PATH to [PATH].

For example, in Bash:

> export LD_LIBRARY_PATH=[PATH]
> ./test


In C-Shell:

> setenv LD_LIBRARY_PATH [PATH]
> ./test

closed account (y8h7M4Gy)
Ok sweet, got it.

Thanks!
No problem.
Topic archived. No new replies allowed.