在 Windows 上的 Code::Blocks 中编译 SDL2 + OpenGL + GLAD 项目:未定义的引用

Compiling SDL2 + OpenGL + GLAD project in Code::Blocks on Windows: undefined references

我正在检查创建使用 OpenGL 代码的 windows 应用程序的不同变体。我对 OpenGL Core Profile(所谓的 "modern OpenGL")特别感兴趣。我正在使用 Code::Blocks 作为我的 IDE。

起初我编写了一个简单的 WinAPI 应用程序。它运行良好,直到我尝试 运行 一些核心代码。这告诉我,如果我想在 Windows 中使用现代 OpenGL,WinAPI 在这里没有用,因为它已在立即模式下修复。

因此,我的下一步是使用 GLFW + GLAD(在 https://glad.dav1d.de/ 网站上生成)制作一个项目。在这种方法中,OpenGL 函数有一些未解析的符号,但 GLAD 解决了这个问题,我的项目就完成了。编译 & 运行 没有问题。

这次我想测试一个SDL变体。我下载了 SDL2 头文件和库,并将它们放入 C::B 安装中的正确文件夹中。起初我编写了一个简单的仅 SDL2 应用程序并且它运行良好,这向我展示了在构建选项中包含和 links 到库不会让编译器阻塞我的代码。

现在我添加了一些使用opengl核心功能的代码。起初编译该代码导致编译器抱怨这些函数。我通过再次添加 GLAD 解决了这些问题,就像我以前的项目一样,它工作正常,但只是编译。当尝试 link 我的程序时,编译器吐出了很多这样的错误:

g++.exe  -o bin\Debug\OpenGL_SDL.exe obj\Debug\glad.o obj\Debug\main.o  -static-libstdc++ -static-libgcc -static -lmingw32 -lSDL2main -lSDL2 -lglew32 -opengl32 -lglu32 -mwindows  
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_hidapi.o): In function `PLATFORM_hid_enumerate':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:354: undefined reference to `__imp_SetupDiGetClassDevsA'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:354: undefined reference to `__imp_SetupDiEnumDeviceInterfaces'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:378: undefined reference to `__imp_SetupDiGetDeviceInterfaceDetailA'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:415: undefined reference to `__imp_SetupDiEnumDeviceInfo'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:420: undefined reference to `__imp_SetupDiGetDeviceRegistryPropertyA'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:565: undefined reference to `__imp_SetupDiDestroyDeviceInfoList'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:427: undefined reference to `__imp_SetupDiGetDeviceRegistryPropertyA'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windows.o): In function `WIN_CoInitialize':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/core/windows/SDL_windows.c:76: undefined reference to `__imp_CoInitializeEx'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windows.o): In function `WIN_CoUninitialize':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/core/windows/SDL_windows.c:95: undefined reference to `__imp_CoUninitialize'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_SetupAPI':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:650: undefined reference to `ImmGetIMEFileNameA'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:663: undefined reference to `ImmGetContext'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:666: undefined reference to `ImmReleaseContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetId':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:584: undefined reference to `ImmGetIMEFileNameA'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:599: undefined reference to `GetFileVersionInfoSizeA'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:603: undefined reference to `GetFileVersionInfoA'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:604: undefined reference to `VerQueryValueA'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `UILess_GetCandidateList':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:1021: undefined reference to `__imp_SysFreeString'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_ClearComposition':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:716: undefined reference to `ImmGetContext'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:720: undefined reference to `ImmNotifyIME'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:724: undefined reference to `ImmNotifyIME'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:725: undefined reference to `ImmReleaseContext'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:722: undefined reference to `ImmSetCompositionStringW'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_Disable':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:404: undefined reference to `ImmAssociateContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `UIElementSink_BeginUIElement':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:1088: undefined reference to `__imp_SysFreeString'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `UIElementSink_UpdateUIElement':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:1115: undefined reference to `__imp_SysFreeString'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_Init':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:348: undefined reference to `__imp_CoCreateInstance'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:363: undefined reference to `ImmGetContext'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:364: undefined reference to `ImmReleaseContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `UILess_SetupSinks':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:1229: undefined reference to `__imp_CoCreateInstance'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_Quit':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:418: undefined reference to `ImmAssociateContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_Enable':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:389: undefined reference to `ImmAssociateContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `WIN_SetTextInputRect':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:244: undefined reference to `ImmGetContext'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:251: undefined reference to `ImmSetCompositionWindow'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:252: undefined reference to `ImmReleaseContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_HandleMessage':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:889: undefined reference to `ImmGetContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetCompositionString':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:732: undefined reference to `ImmGetCompositionStringW'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:737: undefined reference to `ImmGetCompositionStringW'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_HandleMessage':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:927: undefined reference to `ImmReleaseContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetReadingString':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:459: undefined reference to `ImmGetContext'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:531: undefined reference to `ImmReleaseContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_HandleMessage':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:922: undefined reference to `ImmGetContext'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetCandidateList':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:800: undefined reference to `ImmGetCandidateListW'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:804: undefined reference to `ImmGetCandidateListW'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetCompositionString':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:732: undefined reference to `ImmGetCompositionStringW'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:737: undefined reference to `ImmGetCompositionStringW'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_dinputhaptic.o): In function `SDL_DINPUT_HapticInit':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/haptic/windows/SDL_dinputhaptic.c:89: undefined reference to `__imp_CoCreateInstance'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_systimer.o): In function `SDL_SetSystemTimerResolution':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/timer/windows/SDL_systimer.c:57: undefined reference to `__imp_timeBeginPeriod'
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/timer/windows/SDL_systimer.c:51: undefined reference to `__imp_timeEndPeriod'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_systimer.o): In function `SDL_TicksInit':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/timer/windows/SDL_systimer.c:102: undefined reference to `__imp_timeGetTime'
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib\libSDL2.a(SDL_systimer.o): In function `SDL_SetSystemTimerResolution':
/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/timer/windows/SDL_systimer.c:51: undefined reference to `__imp_timeEndPeriod'

我很确定我在 linking 库中搞砸了一些东西。也许他们的顺序错误。也许我想念一些。 可能是另一个错误进入

我在 link 层选项中的图书馆列表是:

mingw32
SDL2main
SDL2
glew32
opengl32
glu32

我不知道“__imp_SetupDiGetClassDevsA”和它的公司来自哪个图书馆,因此我不知道该怎么做☹。

好的,我找到问题所在了。我发现其他人在 Internet 的不同部分问了类似的问题,所以我 post 在这里回答我自己的问题,这样其他人可能会找到它(我希望他们会觉得这很有用)。

这些函数来自不想静态编译的SDL2。 根据此 post Static-linking of SDL2 libraries "proper way" 是 运行 sdl2-configbin\ 文件夹随 SDL2 包一起提供。 运行 它与 --static-libs 应该给我们一个链接器选项列表,以便在 "Build Options... / Linker settings / Other linker options" 对话框中写入。在我的例子中是: -lmingw32 -lSDL2main -lSDL2 -mwindows -Wl,--no-undefined -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid -static-libgcc(这个列表 比我在问题 post 中想的要长)。

我没有早点找到它,因为 sdl2-config 是一个 Bash 文件,而 Code::Blocks 附带的 MinGW 没有 Bash 解释器,所以我不能正常 运行 它。幸运的是,它是一个文本文件(而不是我最初猜测的二进制程序),所以我只是在其中搜索了正确的行。