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

Posted

技术标签:

【中文标题】SDL2 未定义对 MinGW 的几个函数的引用【英文标题】:SDL2 undefined reference to several functions with MinGW 【发布时间】:2016-09-10 16:34:25 【问题描述】:

我的项目目录结构如下:

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

我在这里阅读了很多关于这个问题的问答。但我真的不明白发生了什么。我按照文章中的所有步骤进行操作。我使用的是 Windows 7 64 位,我从 i686-w64-mingw32 paste 获得了包含和 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

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

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

【问题讨论】:

链接器使用了静态库libSDL2.a。对于静态链接,您需要自己指定所有依赖项 (***.com/questions/39110217/…)。所以问题缩小到为什么它不喜欢动态库。 libSDL2.dll.a 在吗?你确定你的abs路径和../lib是同一个目录吗? 嘿!我为自己感到羞耻......我的编译标志是动态链接。我只是把libSDL2.dll.a 并从我的lib 目录中删除了libSDL2.a,一切正常。我的绝对路径是另一个目录,那里有 libSDL2.dll.a 以便我的程序编译。如果你想回答这个问题,我会给你“这个答案对我有帮助”。真可惜。在此先感谢... 但是我试图找到任何关于动态和静态链接的“官方信息”,我没有找到...... 另见***.com/questions/28260245/… 【参考方案1】:

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

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

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

【讨论】:

"默认情况下,如果可以找到动态库,链接器应该更喜欢动态库,或者可以使用 -Wl,-Bstatic 和 -Wl,-Bdynamic 开关手动控制它"这些附加信息对我帮助很大为了完全了解。我想我稍后会阅读更多关于它的内容。再次感谢!

以上是关于SDL2 未定义对 MinGW 的几个函数的引用的主要内容,如果未能解决你的问题,请参考以下文章

mingw 构建错误:未定义对 `__chkstk_ms' 的引用

对“__mingw_vsscanf”的未定义引用

对二叉树类 C++ 函数的未定义引用

将 Winsock2 添加到我的 MinGW-w64 C/C++ 会导致:未定义对“InitializeConditionVariable”的引用

gtkmm 对某些 gtk::builder 函数 add_from_file 的未定义引用

对`__glewCreateShader`的未定义引用[重复]