tags:

views:

388

answers:

3

The linux server at my school is just a bare-bone server, no x-windows, just a command line interface.

I tried to make a graphical c program on that server but found much difficulties. I use SDL library but every time compiling with gcc, it complains:

testcursor.c:(.text+0x1ad): undefined reference to SDL_Init' testcursor.c:(.text+0x1b6): undefined reference to SDL_GetError' testcursor.c:(.text+0x200): undefined reference to `SDL_SetVideoMode' .....

Does anybody knows how to fix the problem? If not, is there anybody who has done graphic program in c in linux, please help! I would appreciate. Thanks.

+4  A: 

Add -lSDL to your compile line.

This tells gcc to link your code to the SDL library.

caf
+2  A: 

You're not linking to the SDL library. Your command should look something like this:

gcc testcursor.c -lsdl

That's assuming you're using the SDL that came with your Linux distro. If you downloaded it and built it by hand, you might need something more complicated, like this:

gcc -I/usr/local/include/sdl testcursor.c -L/usr/local/lib -lsdl

The -I and -L options tell gcc where to look for include files and libraries, respectively. The first command doesn't need them because if you use the stock SDL for your system, they're in the default locations.

Warren Young
Just to clarify, due to the font... that's -i (but capitalized) and -L, for where to look for files. -L (but lowercase) is for linking to a specific library file within the -L (capitalized) directory. (The capital i and lowercase L look different in the code block, but in the plain text font it might have been clear!)
Platinum Azure
I have a difficulty that I am not the root user of my school linux machine. Therefore I have no privilege to install any library (like sdl for example). I think because of that I can't compile with your command above. I wonder if there is any way to work around this problem?
tsubasa
The most obvious solution is to ask your sysadmin to install SDL and whatever other libraries you need.If that can't happen, you can install them under your own account. Download the source, and try configuring it with a command like "./configure --prefix=$HOME". You can then do "make install" without being root, as everything will go into subdirectories of your home directory. Then the gcc command above becomes like "gcc -I$HOME/include..." Beware that if your system is really locked down tight, SDL might not have permission it needs to take over the whole screen, as it does.
Warren Young
+1  A: 

The recommended way of linking SDL on linux is using the sdl-config script.

example:

gcc -c test.c `sdl-config --cflags`
gcc -o test test.o `sdl-config --libs`
./test

or example:

gcc -o test test.c `sdl-config --cflags --libs`

where ` is the back tick character.

pwned