我对编程比较陌生,我决定尝试SDL,但我有点卡住了。我无法在代码块中构建项目,并且我得到了所有SDL函数的“未定义引用”。我在这里看到了很多类似的问题,但没有一个解决方案似乎有所帮助。我已经将\ include \ SDL2和\ lib文件夹添加到搜索目录中,我已将SDL2Main和SDL2添加到链接器选项中的链接库中,我甚至将-mwindows添加到其他链接器选项中。此外,我尝试连接64位版本,但事情变得更糟。
这是我的源代码,几乎完全复制了我开始的教程:
#include <SDL.h>
SDL_Window* g_pWindow = 0;
SDL_Renderer* g_pRenderer = 0;
int main(int argc, char* args[])
{
// initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
// if succeeded create our window
g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN);
// if the window creation succeeded create our renderer
if(g_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
}
}
else
{
return 1; // sdl could not initialize
}
// everything succeeded lets draw the window
// set to black // This function expects Red, Green, Blue and
// Alpha as color values
SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
// clear the window to black
SDL_RenderClear(g_pRenderer);
// show the window
SDL_RenderPresent(g_pRenderer);
// set a delay before quitting
SDL_Delay(5000);
// clean up SDL
SDL_Quit();
return 0;
}
这是构建日志:
mingw32-g++.exe -LC:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib -o bin\Debug\GeometryProject.exe obj\Debug\main.o -mwindows C:\MinGW\lib\libmingw32.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2main.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2.a
obj\Debug\main.o: In function `SDL_main':
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:7: undefined reference to `SDL_Init'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:13: undefined reference to `SDL_CreateWindow'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:17: undefined reference to `SDL_CreateRenderer'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:27: undefined reference to `SDL_SetRenderDrawColor'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:29: undefined reference to `SDL_RenderClear'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:31: undefined reference to `SDL_RenderPresent'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:33: undefined reference to `SDL_Delay'
C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:35: undefined reference to `SDL_Quit'
C:\MinGW\lib\libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
9 errors, 0 warnings (0 minutes, 0 seconds)
还有什么我可以尝试的吗?我真的很想让这个运行起来,并希望得到任何帮助。
答案 0 :(得分:12)
可能的问题是您链接到SDL2库以获得不同的架构。
您应该使用
SDL2-2.0.1\i686-w64-mingw32
而不是
SDL2-2.0.1\x86_64-w64-mingw32
在设置库搜索路径后,使用此表示法链接到库
-lmingw32 -lSDL2main -lSDL2
它更容易阅读。
答案 1 :(得分:2)
不是添加完整路径,而是尝试-LC:/PATH_TO_SDL -lSDL2main -lSDL2
(或只有一个SDL选项);链接器已知lib
和.a
。
(不确定是否必须使用/
或\
)
由于您使用的是C ++,请将include替换为:
extern "C"
{
#include "SDL.h"
}
这告诉编译器将SDL视为C - 而不是C ++ - 代码。