gpt4 book ai didi

c++ - SDL2 undefined reference to several functions with MinGW

转载 作者:可可西里 更新时间:2023-11-01 10:04:05 28 4
gpt4 key购买 nike

我有以下项目目录结构:

/program1
/bin
/include
/SDL2
/lib
/resources
/source
makefile

我在这里阅读了很多关于这个问题的问答。但我真的不明白这是怎么回事。我遵循了文章中的所有步骤。我使用的是 Windows 7 64 位,我从 i686-w64-mingw32 粘贴中获得了包含文件和 lib 文件。如上所述,我将这些文件放在我的目录中。

我在 source 目录中的简单 ma​​in.cpp 代码是:

#include <iostream>
#include "SDL.h"

int main( int argc, char* argv[ ] ) {
if( SDL_Init( SDL_INIT_VIDEO ) != 0 ) {
std::cout << "SDL_Init Error: " << SDL_GetError( ) << std::endl;
return 1;
}

SDL_Quit( );
return 0;
}

source 目录中,我也有一个 ma​​kefile,我正在使用 MSYS,这是 makefile 生成的编译命令:

g++ -Wall -c -std=c++11 -I../include -I../include/SDL2 main.cpp -o ../bin/main.o
g++ ../bin/main.o -L../lib -lmingw32 -lSDL2main -lSDL2 -mwindows -o ../bin/program1.exe

在这两个命令之后,我有这个大错误:(

../lib/libSDL2.a(SDL_windows.o): In function `WIN_CoInitialize':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/core/windows/SDL_windows.c:73: undefined reference to `_imp__CoInitializeEx@8'
../lib/libSDL2.a(SDL_windows.o): In function `WIN_CoUninitialize':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/core/windows/SDL_windows.c:92: undefined reference to `_imp__CoUninitialize@0'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_SetupAPI':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:617: undefined reference to `ImmGetIMEFileNameA@12'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:630: undefined reference to `ImmGetContext@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:633: undefined reference to `ImmReleaseContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetId':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:551: undefined reference to `ImmGetIMEFileNameA@12'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:566: undefined reference to `GetFileVersionInfoSizeA@8'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:570: undefined reference to `GetFileVersionInfoA@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:571: undefined reference to `VerQueryValueA@16'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `UILess_GetCandidateList':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:984: undefined reference to `_imp__SysFreeString@4'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_ClearComposition':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:683: undefined reference to `ImmGetContext@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:687: undefined reference to `ImmNotifyIME@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:691: undefined reference to `ImmNotifyIME@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:692: undefined reference to `ImmReleaseContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_Disable':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:364: undefined reference to `ImmAssociateContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_ClearComposition':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:689: undefined reference to `ImmSetCompositionStringW@24'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:683: undefined reference to `ImmGetContext@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:687: undefined reference to `ImmNotifyIME@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:691: undefined reference to `ImmNotifyIME@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:692: undefined reference to `ImmReleaseContext@8'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:689: undefined reference to `ImmSetCompositionStringW@24'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `UIElementSink_BeginUIElement@12':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:1051: undefined reference to `_imp__SysFreeString@4'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `UIElementSink_UpdateUIElement@8':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:1078: undefined reference to `_imp__SysFreeString@4'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_Init':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:309: undefined reference to `_imp__CoCreateInstance@20'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:323: undefined reference to `ImmGetContext@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:324: undefined reference to `ImmReleaseContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `UILess_SetupSinks':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:1192: undefined reference to `_imp__CoCreateInstance@20'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetId':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:551: undefined reference to `ImmGetIMEFileNameA@12'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:566: undefined reference to `GetFileVersionInfoSizeA@8'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:570: undefined reference to `GetFileVersionInfoA@16'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetReadingString':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:423: undefined reference to `ImmGetContext@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:498: undefined reference to `ImmReleaseContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetId':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:571: undefined reference to `VerQueryValueA@16'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_Quit':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:378: undefined reference to `ImmAssociateContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_Enable':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:349: undefined reference to `ImmAssociateContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_ClearComposition':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:683: undefined reference to `ImmGetContext@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:687: undefined reference to `ImmNotifyIME@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:691: undefined reference to `ImmNotifyIME@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:692: undefined reference to `ImmReleaseContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_Disable':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:364: undefined reference to `ImmAssociateContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_ClearComposition':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:689: undefined reference to `ImmSetCompositionStringW@24'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `WIN_SetTextInputRect':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:202: undefined reference to `ImmGetContext@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:209: undefined reference to `ImmSetCompositionWindow@8'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:210: undefined reference to `ImmReleaseContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_HandleMessage':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:852: undefined reference to `ImmGetContext@4'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetCompositionString':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:699: undefined reference to `ImmGetCompositionStringW@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:704: undefined reference to `ImmGetCompositionStringW@16'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_HandleMessage':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:890: undefined reference to `ImmReleaseContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_ClearComposition':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:683: undefined reference to `ImmGetContext@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:687: undefined reference to `ImmNotifyIME@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:691: undefined reference to `ImmNotifyIME@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:692: undefined reference to `ImmReleaseContext@8'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_HandleMessage':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:885: undefined reference to `ImmGetContext@4'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetCandidateList':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:767: undefined reference to `ImmGetCandidateListW@16'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetCompositionString':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:699: undefined reference to `ImmGetCompositionStringW@16'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:704: undefined reference to `ImmGetCompositionStringW@16'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_ClearComposition':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:689: undefined reference to `ImmSetCompositionStringW@24'
../lib/libSDL2.a(SDL_windowskeyboard.o): In function `IME_GetCandidateList':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/video/windows/SDL_windowskeyboard.c:771: undefined reference to `ImmGetCandidateListW@16'
../lib/libSDL2.a(SDL_xaudio2.o): In function `XAudio2Create':
/usr/local/cross-tools/i686-w64-mingw32/include/xaudio2.h:1273: undefined reference to `_imp__CoCreateInstance@20'
/usr/local/cross-tools/i686-w64-mingw32/include/xaudio2.h:1273: undefined reference to `_imp__CoCreateInstance@20'
/usr/local/cross-tools/i686-w64-mingw32/include/xaudio2.h:1273: undefined reference to `_imp__CoCreateInstance@20'
/usr/local/cross-tools/i686-w64-mingw32/include/xaudio2.h:1273: undefined reference to `_imp__CoCreateInstance@20'
../lib/libSDL2.a(SDL_dinputhaptic.o): In function `SDL_DINPUT_HapticInit':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/haptic/windows/SDL_dinputhaptic.c:98: undefined reference to `_imp__CoCreateInstance@20'
../lib/libSDL2.a(SDL_systimer.o): In function `SDL_SetSystemTimerResolution':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/timer/windows/SDL_systimer.c:57: undefined reference to `_imp__timeBeginPeriod@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/timer/windows/SDL_systimer.c:51: undefined reference to `_imp__timeEndPeriod@4'
../lib/libSDL2.a(SDL_systimer.o): In function `SDL_TicksInit':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/timer/windows/SDL_systimer.c:102: undefined reference to `_imp__timeGetTime@0'
../lib/libSDL2.a(SDL_systimer.o): In function `SDL_SetSystemTimerResolution':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/timer/windows/SDL_systimer.c:51: undefined reference to `_imp__timeEndPeriod@4'
../lib/libSDL2.a(SDL_systimer.o): In function `SDL_GetTicks_REAL':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/timer/windows/SDL_systimer.c:141: undefined reference to `_imp__timeGetTime@0'
../lib/libSDL2.a(SDL_systimer.o): In function `SDL_TicksInit':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/timer/windows/SDL_systimer.c:102: undefined reference to `_imp__timeGetTime@0'
../lib/libSDL2.a(SDL_winmm.o): In function `WINMM_CloseDevice':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:170: undefined reference to `_imp__waveOutUnprepareHeader@12'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:170: undefined reference to `_imp__waveOutUnprepareHeader@12'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:182: undefined reference to `_imp__waveInClose@4'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:187: undefined reference to `_imp__waveOutClose@4'
../lib/libSDL2.a(SDL_winmm.o): In function `WINMM_PlayDevice':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:131: undefined reference to `_imp__waveOutWrite@12'
../lib/libSDL2.a(SDL_winmm.o): In function `SetMMerror':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:106: undefined reference to `_imp__waveOutGetErrorTextW@12'
../lib/libSDL2.a(SDL_winmm.o): In function `WINMM_OpenDevice':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:285: undefined reference to `_imp__waveOutOpen@24'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:331: undefined reference to `_imp__waveOutPrepareHeader@12'
../lib/libSDL2.a(SDL_winmm.o): In function `PrepWaveFormat':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:216: undefined reference to `_imp__waveOutOpen@24'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:214: undefined reference to `_imp__waveInOpen@24'
../lib/libSDL2.a(SDL_winmm.o): In function `WINMM_OpenDevice':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:281: undefined reference to `_imp__waveInOpen@24'
../lib/libSDL2.a(SDL_winmm.o): In function `DetectWaveInDevs':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:57: undefined reference to `_imp__waveInGetNumDevs@0'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:57: undefined reference to `_imp__waveInGetDevCapsW@12'
../lib/libSDL2.a(SDL_winmm.o): In function `DetectWaveOutDevs':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:56: undefined reference to `_imp__waveOutGetNumDevs@0'
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/audio/winmm/SDL_winmm.c:56: undefined reference to `_imp__waveOutGetDevCapsW@12'
../lib/libSDL2.a(SDL_dinputjoystick.o): In function `SDL_DINPUT_JoystickInit':
/Users/slouken/release/SDL/SDL2-2.0.4-source/foo-x86/../src/joystick/windows/SDL_dinputjoystick.c:317: undefined reference to `_imp__CoCreateInstance@20'
collect2.exe: error: ld returned 1 exit status
make: *** [program1.exe] Error 1

我已经尝试将我的 include 切换为“SDL2/SDL.h”并尝试将 include 和 lib 文件放入 mingw 文件夹中,但产生了同样的错误。是不是我的编译命令错了?

编辑:奇怪的是:如果我将编译命令修改为绝对路径,例如 F:/APIs/SDL2/include 和 lib,程序会编译。为什么?

最佳答案

链接器使用了libSDL2.a,这是一个静态库。静态库缺少依赖信息,因此必须手动指定所有其他依赖项。要使用动态库,您的库路径中需要 libSDL2.dll.a(较新的 gcc/mingw 也应该接受 SDL2.dll,即使没有 .dll .a,尽管我从未使用过它)。当然,对于动态链接,您将需要 SDL2.dll 与生成的 .exe 捆绑在一起或在系统目录中以实际启动生成的程序。

您可以将这两个库放在同一个目录中,就像在 SDL2 发行版中一样。默认情况下,如果链接器可以找到一个动态库,它应该更喜欢动态库,或者可以使用 -Wl,-Bstatic-Wl,-Bdynamic 开关手动控制它(如果你想要那个,请先阅读文档 - 这些标志会影响其后指定的所有库)。

(只涵盖mingw;msvc有不同的库格式,SDL2有单独的下载文件)

关于c++ - SDL2 undefined reference to several functions with MinGW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39428235/

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