c++ - SDL 2.0 与 Sublime Text 2 和 MinGW 的链接问题

标签 c++ mingw sublimetext2 sdl

我在使用 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]

最佳答案

我似乎(终于)解决了我的问题。更新到最新版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"]

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

关于c++ - SDL 2.0 与 Sublime Text 2 和 MinGW 的链接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18807765/

相关文章:

python - 从 Sublime Text "view"末尾开始的正则表达式匹配

c++ - 使所有派生模板类成为 C++ 中其他类的 friend

C++ 避免全局变量和单例

c++ - 跨编译器的诊断不一致,以缩小非类型模板参数中的转换范围

c++ - 使用 QtSDK 中的 mingw 构建 boost

sublimetext2 - 如何为 Sublime Text 2 安装 VHDL 语法高亮?

c++ - c++ 接口(interface)必须遵守五法则吗?

c++ - 在 Windows 上链接可执行文件的正确方法是什么?

gcc - vscode使用workspaceRoot还是workspaceFolder?

sublimetext2 - 在 SublimeLinter 中忽略 XML 命名空间 (TAL)