gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:03:24 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com