当G ++没有给出错误时,对带有LD的SDL2_Init的未定义引用

问题描述

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

我正在尝试使用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

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

解决方法

MinGW上出现神秘的undefined reference错误的常见原因是使用x32编译器为x64编译的库,反之亦然。 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

(我知道)此标志的唯一目的是防止您的应用自动为发布版本自动打开终端窗口。