2

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

我试图编译的代码(来自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 text 文件从以下位置构建:

{
 "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]
4

2 回答 2

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"]

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

于 2013-11-06T08:08:28.990 回答
-1

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

于 2013-09-15T06:01:28.203 回答