Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) undefined reference to 'SDL_Init'

 

Link error.

 

 

 

 

 

Full error message

 

/MyFolder/main.o:: In function 'main':
/MyFolder/main.cpp:7: error: undefined reference to 'SDL_Init'
/MyFolder/main.cpp:9: error: undefined reference to 'SDL_GetError'
/MyFolder/main.cpp:12: error: undefined reference to 'SDL_Quit'
:: error: collect2: ld returned 1 exit status

 

 

 

 

 

 

Cause

 

IDE: Qt Creator 2.0.0

Project type: Qt4 Console Application

Selected required modules: QtCore

Compiler: G++ 4.4.1

Additional libraries: SDL

 

The following source code was used, from the SDL tutorial:

 

#include <cstdlib>
#include <iostream>
#include <SDL/SDL.h>

int main()
{
  if ( SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0 )
  {
    std::cerr << "Unable to init SDL: " << SDL_GetError() << '\n';
    return 1;
  }
  std::atexit(SDL_Quit);
}

 

The following project file was used:

 

#-------------------------------------------------
#
# Project created by QtCreator 2010-07-17T14:57:33
#
#-------------------------------------------------

QT += core

QT -= gui

TARGET = TestSdl
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

 

 

 

 

 

Solution

 

You need to link against the SDL library. Add the following line to your project file:

 

LIBS += -L/usr/local/lib -lSDL

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict