SDL 2.0将问题与Sublime Text 2和MinGW联系起来

时间:2013-09-15 00:33:15

标签: c++ mingw sublimetext2 sdl

我在使用mingw让sublime text 2中的SDL 2.0为我工作时遇到了一些问题。

我试图编译的代码(来自lazy foo的SDL 2.0教程):

#include <SDL2/SDL.h>
#include <stdio.h>

// scrren dimension constraints
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main(int argc, char *argv[]){
    //window
    SDL_Window* window = NULL;
    SDL_Surface* screenSurface = NULL;

    //Initialize sdl
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
    }
    else{
        //create window
        window = SDL_CreateWindow("SDL 1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (window == NULL){
            printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        }
        else
        {
            //Get window surface
            screenSurface = SDL_GetWindowSurface(window);

            //Fill the surface white
            SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );

            //Update the surface
            SDL_UpdateWindowSurface( window );

            //Wait two seconds
            SDL_Delay(2000);
        }
    }
    //destrow window
    SDL_DestroyWindow(window);

    //quit
    SDL_Quit();
    return 0;
}

文件Sublime文本构建于:

{
 "path": "D:/MinGW/bin",
 "working_dir": "${file_path}",
 "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}.exe" ,"-std=c++0x", "-lSDL2", "-lSDL2main"],
 "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
 "selector": "source.c, source.c++, source.cpp",

 "variants":
 [
  {
   "name": "Run",
   "working_dir": "${file_path}",
   "cmd": ["${file_base_name}.exe"]
  }
 ]
}

错误讯息:

D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0x1c): undefined reference to `SDL_Init'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0x28): undefined reference to `SDL_GetError'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0x71): undefined reference to `SDL_CreateWindow'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0x7f): undefined reference to `SDL_GetError'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0x9c): undefined reference to `SDL_GetWindowSurface'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0xc5): undefined reference to `SDL_MapRGB'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0xdc): undefined reference to `SDL_FillRect'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0xe7): undefined reference to `SDL_UpdateWindowSurface'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0xf3): undefined reference to `SDL_Delay'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0xfe): undefined reference to `SDL_DestroyWindow'
D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o:main.cpp:(.text+0x103): undefined reference to `SDL_Quit'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: D:\Users\ForUs\AppData\Local\Temp\ccEEP5Hi.o: bad reloc address 0x1b in section `.text$printf[_printf]'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
[Finished in 0.4s with exit code 1]

3 个答案:

答案 0 :(得分:2)

我似乎(最终)解决了我的问题。更新到最新版本的MinGW和SDL2.0并更改

"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}.exe" ,"-std=c++0x", "-lSDL2", "-lSDL2main"],

 "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}.exe","-lmingw32", "-lSDL2main", "-lSDL2"]

解决了我的问题。感谢所有试图提供帮助的人的建议。

答案 1 :(得分:-1)

您的链接器设置“-lSDL2”,“ - lSDL2main”可能是错误的方式,尝试将它们反转

答案 2 :(得分:-2)

听起来您的SDL库不在gcc's default search path(s)

-L/absolute/path/to/sdl/libs"-std=c++0x"之间添加"-lSDL2",告诉gcc 确切地您的库所在的位置。