Skip to main content
GameDev.net gamedev.net
🔒 Locked

Linking problems

Started by soehrimnir Nov 22, 2000 at 4:31 AM 2 replies 500+ views
Original Post
soehrimnir
soehrimnir
Hi, I''m just new to Linux programming and tried to make a little "game" with SDL. I use KDevelop and entered the following little code:
  
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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


int main(int argc, char *argv[]) {

  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    cout << "Unable to initialize SDL.\n";
    cout << "Reason: " << SDL_GetError();
    exit(1);
  }

  atexit(SDL_Quit);

  cout << "Initialization succeeded!!";

  return 0;
}
  
This compiles, but I can''t link it. I get: undefined reference to ''SDL_Init''. Then I thought. I probably have to add some lib somewhere. So I found you could set the project properties. I typed in the lib path of SDL: /usr/lib/libSDL.so. I don''t know if this is the right one, but when I link now it gives the following undefined references: ''pthread_create'', ''pthread_cancel'', ''sem_destroy'', ''sem_wait'', ... and a lot more (pthread_* and sem_*). Do I need to include other libs? I''ve installed both the development and binary runtime of SDL. Any help would be appreciated. Btw, I''ve also tried to link in the console with ld, but it gave the same troubles.
snowmoon
snowmoon
On the linking stage you need these options....

gcc -o binary myfile.o -lSDL -lpthread

Should take care of the problem
soehrimnir
soehrimnir
Thank you very much!! It works now. Now I can finally make some games in Linux .
Shelrem
Shelrem
For your further reference you can type the following:

#sdl-config --libs
-L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL -lpthread

#sdl-config --cflags
-I/usr/local/include -I/usr/local/include/SDL -D_REENTRANT

YMMV. That's the whole point. That's why my Makefiles always look like this:

CC = g++
LIBS = display.o input.o
#Obtain the flags required to compile with SDL
SDL_CFLAGS = `sdl-config --cflags`
SDL_LIBS = `sdl-config --libs`
#Any other compiler options
CC_OPTIONS = -Wall -O2 -DDEBUG -g
#
# First and default rule
#
main: main.cpp config.h ${LIBS}
${CC} ${CC_OPTIONS} ${SDL_CFLAGS} ${SDL_LIBS} -o main ${LIBS} main.cpp
#
display.o: display.cpp display.hpp config.h
${CC} ${CC_OPTIONS} ${SDL_CFLAGS} -o display.o -c display.cpp
#
input.o: input.cpp input.hpp config.h
${CC} ${CC_OPTIONS} ${SDL_CFLAGS} -o input.o -c input.cpp




See, this way sdl-config gives you the correct flags on any configuration.


-benc


Edited by - shelrem on November 22, 2000 2:02:43 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.