2011-08-28 44 views
4

以下代碼非常簡單。它只是使用SDL在背景上繪製圖像。我之前能夠運行的程序做得很好,參考了所有內容,沒有任何問題。SDL鏈接器錯誤

但是,現在,編譯器抱怨。

總之,這裏的代碼:

#include <iostream> 
#include <string> 
#include "SDL/SDL.h" 

const int SCREEN_WIDTH = 640; 
const int SCREEN_HEIGHT = 480; 
const int SCREEN_BPP = 32; 

using std::string; 

SDL_Surface * load_image(std::string filename) 
{ 
    SDL_Surface * loadedImage = NULL; 

    SDL_Surface * optimizedImage = NULL; 

    loadedImage = SDL_LoadBMP(filename.c_str()); 

    if (loadedImage != NULL) 
    { 
     optimizedImage = SDL_DisplayFormat(loadedImage); 

     SDL_FreeSurface(loadedImage); 
    } 

    return optimizedImage; 
} 

void apply_surface(int x, int y, SDL_Surface * source, SDL_Surface * destination) 
{ 
    SDL_Rect offset; 

    //Give offsets to the rectangle 
    offset.x = x; 
    offset.y = y; 

    SDL_BlitSurface(source, NULL, destination, &offset); 
} 

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

    if (SDL_Init(SDL_INIT_EVERYTHING) == -1) 
    { 
     return 1; 
    } 
    else 
    { 

     SDL_Surface * screen = SDL_SetVideoMode(SCREEN_WIDTH, 
            SCREEN_HEIGHT, 
            SCREEN_BPP,          
            SDL_SWSURFACE); 

     if (screen == NULL) 
     { 
      return 1; 

     } 
     else 
     { 
      SDL_WM_SetCaption("Hello World", NULL); 

      SDL_Surface * message = load_image("hello.bmp"); 
      SDL_Surface * background = load_image("background.bmp"); 

      apply_surface(0, 0, background, screen); 
      apply_surface(320, 0, background, screen); 
      apply_surface(0, 240, background, screen); 
      apply_surface(320, 240, background, screen); 

      if (SDL_Flip(screen) == -1) 
      { 
       return 1; 
      } 
     } 


    } 

    return 0; 

} 

而且我的輸出:

[email protected]:~/code/cpp$ g++ main.o 
main.o: In function `load_image(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': 
main.cpp:(.text+0x2b): undefined reference to `SDL_RWFromFile' 
main.cpp:(.text+0x3b): undefined reference to `SDL_LoadBMP_RW' 
main.cpp:(.text+0x4f): undefined reference to `SDL_DisplayFormat' 
main.cpp:(.text+0x5d): undefined reference to `SDL_FreeSurface' 
main.o: In function `apply_surface(int, int, SDL_Surface*, SDL_Surface*)': 
main.cpp:(.text+0x97): undefined reference to `SDL_UpperBlit' 
main.o: In function `main': 
main.cpp:(.text+0xaf): undefined reference to `SDL_Init' 
main.cpp:(.text+0xe7): undefined reference to `SDL_SetVideoMode' 
main.cpp:(.text+0x110): undefined reference to `SDL_WM_SetCaption' 
main.cpp:(.text+0x24c): undefined reference to `SDL_Flip' 
collect2: ld returned 1 exit status 
+0

詳細信息(http://wiki.libsdl.org/moin.cgi/FAQLinux#How_do_I_add_SDL_to_my_project.3F)。 –

回答

6

你不是在SDL庫鏈接。試試這個:在[SDL FAQ]

g++ main.o `sdl-config --libs`