SDL2 链接错误?对 SDL_main 的未定义引用

SDL2 Linking errors? Undefined reference to SDL_main

我正在尝试弄清楚如何在 VSCODE 上使用 SDL2。我有基本代码并且 windows 可以正常工作,但是当我添加其他函数和文件时,它似乎不想编译。我是新手,但我想我在我的 headers 和我的构建中包含了必要的东西。

为主

#include "../include/SDL2/SDL.h"
#include "assets/cleanup.h"
#include "assets/res_path.h"
#include "assets/drawing_functions.h"
#include "../include/SDL2/SDL_mixer.h"
#include <iostream>

对于build.bat我有

    g++ -Isrc/include -Lsrc/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2
    g++ -Isrc/include -Lsrc/lib -o cleanup assets/cleanup.cpp -lmingw32 -lSDL2main -lSDL2
    g++ -Isrc/include -Lsrc/lib -o drawing_functions assets/drawing_functions.cpp -lmingw32 -lSDL2main -lSDL2
    g++ -Isrc/include -Lsrc/lib -o res_path assets/res_path.cpp -lmingw32 -lSDL2main -lSDL2
    g++ -Isrc/include -Lsrc/lib -o randomNumber assets/randomNumber.cpp -lmingw32 -lSDL2main -lSDL2

注意:它在没有额外的 cpp 文件的情况下工作得很好。如果我只有 main 并删除了我的添加,那 运行 没问题。我想知道是什么导致它坏了。

对于res_path

#include <iostream>
#include <string>
#include "../include/SDL2/SDL.h"

对于 random_number 它只是 ctime 和 cstdlib

绘图功能

#include <iostream>
#include "../include/SDL2/SDL.h"
#include "../include/SDL2/SDL_image.h"
#include "../include/SDL2/SDL_ttf.h"
#include "res_path.h"
#include "cleanup.h"

终于要清理了

#include <utility>
#include "../include/SDL2/SDL.h"

这是我一直在尝试调试的错误

文本错误:

C:\Users\PC\Desktop\C++ProjectsD Game>g++ -Isrc/include -Lsrc/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x92): undefined reference to `cleanup(SDL_Window*)'
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x145): undefined reference to `IMG_Init'
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x18b): undefined reference to `TTF_Init'
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x1ec): undefined reference to `Mix_OpenAudio'
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x261): undefined reference to `getResourcePath(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'  
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x2a1): undefined reference to `loadTexture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, SDL_Renderer*)'
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x2f4): undefined reference to `renderTexture(SDL_Texture*, SDL_Renderer*, int, int, SDL_Rect*)'
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x30c): undefined reference to `cleanup(SDL_Renderer*)'
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x317): undefined reference to `cleanup(SDL_Window*)'
C:\Users\PC\AppData\Local\Temp\ccOXUYZZ.o:main.cpp:(.text+0x322): undefined reference to `cleanup(SDL_Texture*)'
collect2.exe: error: ld returned 1 exit status

C:\Users\PC\Desktop\C++ProjectsD Game>g++ -Isrc/include -Lsrc/lib -o cleanup assets/cleanup.cpp -lmingw32 -lSDL2main -lSDL2
src/lib/libSDL2main.a(SDL_windows_main.o): In function `main_getcmdline':
/Users/valve/release/SDL/SDL2-2.0.14-source/foo-x86/../src/main/windows/SDL_windows_main.c:71: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status

C:\Users\PC\Desktop\C++ProjectsD Game>g++ -Isrc/include -Lsrc/lib -o drawing_functions assets/drawing_functions.cpp -lmingw32 -lSDL2main -lSDL2
C:\Users\PC\AppData\Local\Temp\ccSkQs61.o:drawing_functions.cpp:(.text+0x19): undefined reference to `IMG_LoadTexture'
C:\Users\PC\AppData\Local\Temp\ccSkQs61.o:drawing_functions.cpp:(.text+0x62): undefined reference to `IMG_Load'
C:\Users\PC\AppData\Local\Temp\ccSkQs61.o:drawing_functions.cpp:(.text+0x108): undefined reference to `cleanup(SDL_Surface*)'
C:\Users\PC\AppData\Local\Temp\ccSkQs61.o:drawing_functions.cpp:(.text+0x1ea): undefined reference to `TTF_OpenFont'
C:\Users\PC\AppData\Local\Temp\ccSkQs61.o:drawing_functions.cpp:(.text+0x242): undefined reference to `TTF_RenderText_Blended'
C:\Users\PC\AppData\Local\Temp\ccSkQs61.o:drawing_functions.cpp:(.text+0x256): undefined reference to `TTF_CloseFont'
C:\Users\PC\AppData\Local\Temp\ccSkQs61.o:drawing_functions.cpp:(.text+0x2d8): undefined reference to `TTF_CloseFont'
C:\Users\PC\AppData\Local\Temp\ccSkQs61.o:drawing_functions.cpp:(.text+0x338): undefined reference to `TTF_RenderText_Blended'
src/lib/libSDL2main.a(SDL_windows_main.o): In function `main_getcmdline':
/Users/valve/release/SDL/SDL2-2.0.14-source/foo-x86/../src/main/windows/SDL_windows_main.c:71: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status

C:\Users\PC\Desktop\C++ProjectsD Game>g++ -Isrc/include -Lsrc/lib -o res_path assets/res_path.cpp -lmingw32 -lSDL2main -lSDL2
src/lib/libSDL2main.a(SDL_windows_main.o): In function `main_getcmdline':
/Users/valve/release/SDL/SDL2-2.0.14-source/foo-x86/../src/main/windows/SDL_windows_main.c:71: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status

C:\Users\PC\Desktop\C++ProjectsD Game>g++ -Isrc/include -Lsrc/lib -o randomNumber assets/randomNumber.cpp -lmingw32 -lSDL2main -lSDL2
src/lib/libSDL2main.a(SDL_windows_main.o): In function `main_getcmdline':
/Users/valve/release/SDL/SDL2-2.0.14-source/foo-x86/../src/main/windows/SDL_windows_main.c:71: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status

您可以使用 g++ 以两种方式构建您的程序:

  • 分两个阶段,首先将单个 .cpp 文件作为翻译单元编译为目标文件 .o,然后在 g++ 的另一个调用中将 link 将它们组合成一个可执行文件

  • 或者您可以使用 g++ 的单个调用来编译和 link 可执行文件。

您目前正试图以某种方式混合这两种模式。对于单次调用构建,您应该只调用 g++ 一次并将所有 .cpp 文件添加到其中:

g++ -Isrc/include -Lsrc/lib -o my_program main.cpp assets/cleanup.cpp assets/drawing_functions.cpp [...] -lmingw32 -lSDL2main -lSDL2

这将输出文件名为my_program的程序。

您当前的构建命令构建 多个 个独立的可执行文件,每个可执行文件仅包含 .cpp 个文件之一作为翻译单元。


此外,IMG_Load 等是您未包含在调用中的不同库的一部分。您需要添加 -lSDL_image(并可能先安装该库)。