【问题标题】:Undefined Reference to `SDL2_Init` with LD when G++ is not giving an error当 G++ 未给出错误时,未定义对 LD 的“SDL2_Init”的引用
【发布时间】:2021-01-31 07:03:38
【问题描述】:

(仅供参考,是的,我确实知道存在类似的问题,但我已经尝试了所有答案。)

我正在尝试使用 MinGW 设置 SDL2,并且除了一件事之外我已经设置了所有内容。每当我尝试编译 G++ 时都不会出错,但 LD 会给我一个:undefined reference to SDL_Init 错误。

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\cc28dE2R.o: in function `SDL_main':
C:\Users\user\Documents\SDL2/src/main.cpp:11: undefined reference to `SDL_Init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\Documents\SDL2/src/main.cpp:14: undefined reference to `SDL_GetError'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\Documents\SDL2/src/main.cpp:18: undefined reference to `IMG_Init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\user\Documents\SDL2/src/main.cpp:20: undefined reference to `SDL_GetError'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command C:\MinGW\bin\g++.exe -g src\*.cpp -o build\game.exe -ID:C:\MinGW\include -LD:C:\MinGW\lib -mwindows -lmingw32 -lSDL2main -lSDL2_image -lSDL2" terminated with exit code: 1.

我试图移动 args。做-mwindows,添加-lmingw32。我仍然不断收到相同的错误..我能够修复 WinMain 错误..但我无法修复其他所有问题。

我正在运行的编译命令:

C:\MinGW\bin\g++.exe -g src\*.cpp -o build\game.exe -ID:C:\MinGW\include -LD:C:\MinGW\lib -mwindows -lSDL2main -lSDL2_image -lSDL2

如果我能得到任何帮助,那就太棒了

【问题讨论】:

    标签: c++ g++ mingw ld sdl-2


    【解决方案1】:

    MinGW 上神秘的undefined reference 错误的常见原因是使用为 x64 编译的库和 x32 编译器,反之亦然。 SDL 提供 x32 和 x64 两种版本,请尝试其他版本。

    我能够修复 WinMain 错误

    请注意,修复undefined reference to WinMain@16 的预期方法是添加#define SDL_MAIN_HANDLED执行#undef main。一旦你获得了正确的库(见第一段),错误应该会自行消失。

    确保使用int main(int, char**)(而不是int main()void main()),否则将无法正常工作。

    -ID:C:\MinGW\include -LD:C:\MinGW\lib

    你不应该需要那些标志。这些目录将被自动搜索。

    由于您没有指定任何其他目录,我假设您将 SDL 文件直接放在编译器目录中,这不是一个好习惯,IMO。

    我认为D:C: 是错字?

    -mwindows

    这个标志(据我所知)的唯一目的是防止您的应用自动为自己打开终端窗口,用于发布版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      相关资源
      最近更新 更多