SDL2未定义对几个函数的引用

SDL2 undefined reference to several functions with MinGW

本文关键字:几个 函数 引用 未定义 SDL2      更新时间:2023-10-16

我有以下项目目录结构:

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

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

我的简单的main.cpp代码在source目录是:

#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目录下,我也有一个makefile,我正在使用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

我已经尝试将我的包含切换到"SDL2/SDL.h",并试图将包含和lib文件放在mingw文件夹中,并产生相同的错误。我的编译命令是错误的吗?

EDIT:奇怪的是:如果我将编译命令修改为绝对路径,例如F:/api/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有单独的下载文件)