Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SDL2 Linking errors? Undefined reference to SDL_main

I am trying to figure out how to use SDL2 on VSCODE. I’ve got basic code and windows working but when I add additional functions and files, it doesn’t seem to want to compile. I’m new to this but I thought I included the necessary things in my headers and in my build.

For the main

#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>

For the build.bat I have

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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

note: it works just fine without the additional cpp files. If I just had main and deleted my additions it’d run fine. I’m wondering whats causing it to break.

For res_path

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

for random_number its just ctime and cstdlib

for drawingfunctions

#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"

Finally for cleanup

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

Here are the errors that I’ve been trying to debug
Errors

Errors as text:

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

>Solution :

You can use g++ to build your program in two ways:

  • In two stages, first by compiling the individual .cpp files as translation units to object files .o and then in another invocation of g++ to link them together into an executable

  • Or you can use a single invocation of g++ to compile and link the executable.

You are currently trying to mix the two modes somehow. For the single-invocation build you should call g++ only once and add all .cpp files to it:

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

This will output the program with the file name my_program.

Your current build commands build multiple independent executables, each one containing only one of the .cpp files as a translation unit.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading