我是个相对新手的程序员,决定尝试一下SDL,但卡住了。在Codeblocks中我无法构建项目,所有SDL函数都出现"undefined reference"。我在这里看到了很多类似的问题,但似乎没有一个解决方案能帮到我。我已经将\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)
还有什么其他我可以尝试的吗?我真的很想让它能够运行,也会非常感激任何帮助。