簡體   English   中英

SDL2 鏈接錯誤? 對 SDL_main 的未定義引用

[英]SDL2 Linking errors? Undefined reference to SDL_main

我想弄清楚如何在 VSCODE 上使用 SDL2。 我有基本代碼和 windows 工作,但是當我添加額外的函數和文件時,它似乎不想編譯。 我是新手,但我認為我在標題和構建中包含了必要的內容。

對於主要

#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++Projects\2D 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++Projects\2D 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++Projects\2D 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++Projects\2D 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++Projects\2D 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文件編譯為 object 文件.o的翻譯單元,然后在另一個g++調用中將它們鏈接在一起成為可執行文件

  • 或者您可以使用g++的單個調用來編譯和鏈接可執行文件。

您目前正試圖以某種方式混合這兩種模式。 對於單調用構建,您應該只調用一次g++並將所有.cpp文件添加到其中:

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

這將 output 文件名為my_program的程序。

您當前的構建命令會構建多個獨立的可執行文件,每個可執行文件僅包含一個.cpp文件作為翻譯單元。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM