2013-08-26 155 views
3

我試圖建立一個基於MinGW並使用SDL的C++項目。當我嘗試編譯我的程序時,g ++會爲我使用的每個SDL函數指定undefined reference to 'SDL_Function'SDL2未定義的函數引用

庫/ SDL2:

#include "SDL.h" 

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

    SDL_Window *window = SDL_CreateWindow(
     "Hello World", 
     SDL_WINDOWPOS_CENTERED, 
     SDL_WINDOWPOS_CENTERED, 
     640, 480, 0 
    ); 

    SDL_Delay(3000); 
    SDL_DestroyWindow(window); 
    SDL_Quit(); 
    return 0; 
} 

我用耙簡化:從SDL網站

源/ Main.cpp的SDL2-devel的-2.0.0-mingw.tar.gz內容構建過程中一點點,它產生的是條命令:

g++ -Wall -ILib/SDL2/i686-w64-mingw32/include/SDL2 -Dmain=SDL_main -LLib/SDL2/i686-w64-mingw32/lib -lmingw32 -lSDL2main -lSDL2 -mwindows Source/Main.cpp -o Build/sdltest.exe

而且這裏是G ++說:

Main.cpp:(.text+0xe): undefined reference to `SDL_Init'` 
Main.cpp:(.text+0x42): undefined reference to `SDL_CreateWindow'` 
Main.cpp:(.text+0x51): undefined reference to `SDL_Delay'` 
Main.cpp:(.text+0x5c): undefined reference to `SDL_DestroyWindow'` 
Main.cpp:(.text+0x61): undefined reference to `SDL_Quit'` 
ld.exe: bad reloc address 0x20 in section `.eh_frame'` 
ld.exe: final link failed: Invalid operation` 
collect2.exe: error: ld returned 1 exit status` 

它看起來像一個普通初學者的問題,但我想我通過了所有的故障排除要點:

  • 主要功能有int argc, char *argv[]
  • 我使用所有的SDL-配置(-Dmain = SDL_main標誌 - lmingw32 -lSDL2main -lSDL2 -mwindows)
  • 我試圖使用32位的MinGW
  • 時使用 '的i686-W64-mingw32的' 版本應該所有指定的路徑似乎正確

任何線索怎麼回事?

+0

如何確保鏈接器能夠找到定義? –

+1

感謝您的回覆。我該怎麼做? – VCake

+1

我記得絆倒了一個類似於這個奇怪的問題,但我不記得確切的細節。嘗試改變輸入參數的順序,例如g ++ -o sdltext.exe $(sdlconfig --libs) – zeroc8

回答

12

嘗試改變輸入則params的序列:

我絆倒在此之前(在Linux上):

這個調用產生一個錯誤:

g++ $(pkg-config --cflags --libs sdl2) sdl2test.cpp 

sdl2test.cpp:(.text+0x11): undefined reference to `SDL_Init' 
sdl2test.cpp:(.text+0x20): undefined reference to `SDL_GetError' 
sdl2test.cpp:(.text+0x34): undefined reference to `SDL_Quit' 

這工作:

g++ sdl2test.cpp $(pkg-config --cflags --libs sdl2) 
+0

在Windows 8(10)上使用SDL_mixer和Qt5時,我遇到同樣的問題。我如何在Windows上添加pgk-config? – aviit

0

我改變了輸入參數的順序,它的工作,把SDL2main之前SLD2