Fel*_*leg 2 c++ opengl gcc codeblocks sdl-2
我正在检查创建使用 OpenGL 代码的 Windows 应用程序的不同变体。我对 OpenGL Core Profile(所谓的“现代 OpenGL”)特别感兴趣。我正在使用 Code::Blocks 作为我的 IDE。
\n\n首先,我编写了一个简单的 WinAPI 应用程序。它工作得很好,直到我尝试运行一些核心代码。这告诉我,如果我想在 Windows 中使用现代 OpenGL,WinAPI 在这里不起作用,因为它是通过立即模式修复的。
\n\n因此,我的下一步是使用 GLFW + GLAD 制作一个项目(在https://glad.dav1d.de/站点上生成)。在这种方法中,OpenGL 函数存在一些未解析的符号,但 GLAD 修复了这个问题,我的项目也完成了。编译运行没有问题。
\n\n这次我想测试 SDL 变体。我下载了 SDL2 标头和库,并将它们放入 C::B 安装中的正确文件夹中。起初,我编写了一个简单的仅 SDL2 应用程序,它运行良好,这表明我在构建选项中包含库和链接不会让编译器因我的代码而阻塞。
\n\n现在我添加了一些使用 opengl 核心功能的代码。最初编译该代码会导致编译器抱怨这些函数。我通过再次添加 GLAD 解决了这些问题,就像我之前的项目一样,它工作正常,但只能编译。当尝试链接我的程序时,编译器会抛出很多这样的错误:
\n\ng++.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 \nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:354: undefined reference to `__imp_SetupDiGetClassDevsA\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:354: undefined reference to `__imp_SetupDiEnumDeviceInterfaces\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:378: undefined reference to `__imp_SetupDiGetDeviceInterfaceDetailA\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:415: undefined reference to `__imp_SetupDiEnumDeviceInfo\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:420: undefined reference to `__imp_SetupDiGetDeviceRegistryPropertyA\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:565: undefined reference to `__imp_SetupDiDestroyDeviceInfoList\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/hidapi/windows/hid.c:427: undefined reference to `__imp_SetupDiGetDeviceRegistryPropertyA\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/core/windows/SDL_windows.c:76: undefined reference to `__imp_CoInitializeEx\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/core/windows/SDL_windows.c:95: undefined reference to `__imp_CoUninitialize\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:650: undefined reference to `ImmGetIMEFileNameA\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:663: undefined reference to `ImmGetContext\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:666: undefined reference to `ImmReleaseContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:584: undefined reference to `ImmGetIMEFileNameA\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:599: undefined reference to `GetFileVersionInfoSizeA\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:603: undefined reference to `GetFileVersionInfoA\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:604: undefined reference to `VerQueryValueA\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:1021: undefined reference to `__imp_SysFreeString\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:716: undefined reference to `ImmGetContext\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:720: undefined reference to `ImmNotifyIME\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:724: undefined reference to `ImmNotifyIME\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:725: undefined reference to `ImmReleaseContext\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:722: undefined reference to `ImmSetCompositionStringW\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:404: undefined reference to `ImmAssociateContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:1088: undefined reference to `__imp_SysFreeString\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:1115: undefined reference to `__imp_SysFreeString\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:348: undefined reference to `__imp_CoCreateInstance\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:363: undefined reference to `ImmGetContext\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:364: undefined reference to `ImmReleaseContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:1229: undefined reference to `__imp_CoCreateInstance\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:418: undefined reference to `ImmAssociateContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:389: undefined reference to `ImmAssociateContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:244: undefined reference to `ImmGetContext\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:251: undefined reference to `ImmSetCompositionWindow\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:252: undefined reference to `ImmReleaseContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:889: undefined reference to `ImmGetContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:732: undefined reference to `ImmGetCompositionStringW\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:737: undefined reference to `ImmGetCompositionStringW\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:927: undefined reference to `ImmReleaseContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:459: undefined reference to `ImmGetContext\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:531: undefined reference to `ImmReleaseContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:922: undefined reference to `ImmGetContext\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:800: undefined reference to `ImmGetCandidateListW\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:804: undefined reference to `ImmGetCandidateListW\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:732: undefined reference to `ImmGetCompositionStringW\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/video/windows/SDL_windowskeyboard.c:737: undefined reference to `ImmGetCompositionStringW\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/haptic/windows/SDL_dinputhaptic.c:89: undefined reference to `__imp_CoCreateInstance\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/timer/windows/SDL_systimer.c:57: undefined reference to `__imp_timeBeginPeriod\'\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/timer/windows/SDL_systimer.c:51: undefined reference to `__imp_timeEndPeriod\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/timer/windows/SDL_systimer.c:102: undefined reference to `__imp_timeGetTime\'\nC:/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\':\n/Users/valve/release/SDL/SDL2-2.0.12-source/foo-x64/../src/timer/windows/SDL_systimer.c:51: undefined reference to `__imp_timeEndPeriod\'\nRun Code Online (Sandbox Code Playgroud)\n\n我很确定我在链接库中弄乱了一些东西。也许它们的顺序是错误的。也许我想念一些。或者 这可能是另一个错误。
\n\n我在链接器选项中的库列表是:
\n\nmingw32\nSDL2main\nSDL2\nglew32\nopengl32\nglu32\nRun Code Online (Sandbox Code Playgroud)\n\n我不知道“__imp_SetupDiGetClassDevsA”及其公司来自哪个库,因此我不知道该怎么做\xe2\x98\xb9。
\n好的,我找到问题出在哪里了。我发现其他人在互联网的不同部分提出了类似的问题,因此我在这里发布了我自己问题的答案,以便其他人可能会找到它(我希望他们会发现这很有用)。
这些函数来自 SDL2,它不想静态编译。根据这篇文章《SDL2 库的静态链接》,sdl2-config “正确的方法”是从bin\SDL2 包附带的文件夹运行。运行它--static-libs应该会给我们一个要在“构建选项.../链接器设置/其他链接器选项”对话框中写入的链接器选项列表。就我而言,它是:(
-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这比我在问题帖子中想象的要长得多)。
我之前没有找到它,因为sdl2-config它是一个 Bash 文件,而 Code::Blocks 附带的 MinGW 没有附带 Bash 解释器,所以我无法正常运行它。幸运的是,它是一个文本文件(而不是我最初猜测的二进制程序),所以我只是搜索它以找到正确的行。