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