undefined reference to SDL_Init
Asked Answered
S

6

9

I started using SDL today and had some trouble before, now I got it running but it won't let me init it.

This is my code:

#include <iostream>
#include "SDL.h"
#undef main

using namespace std;

int main(){
    if(SDL_Init(SDL_INIT_EVERYTHING)<0){
        cout << "error starting sdl" << endl;
    }
    return 0;
}

This the build log:

-------------- Build: Debug in Graphics (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -g -std=c++11 -IC:\Users\73638G75MA\Documents\SDL2-2.0.3\x86_64-w64-mingw32\include\SDL2 -I"C:\Users\73638G75MA\Documents\C++ projects\Graphics" -c "C:\Users\73638G75MA\Documents\C++ projects\Graphics\main.cpp" -o obj\Debug\main.o
mingw32-g++.exe -LC:\Users\73638G75MA\Documents\SDL2-2.0.3\x86_64-w64-mingw32\lib -o bin\Debug\Graphics.exe obj\Debug\main.o  -lmingw32 -lSDL2main -lSDL2  
obj\Debug\main.o: In function `main':
C:/Users/73638G75MA/Documents/C++ projects/Graphics/main.cpp:8: undefined reference to `SDL_Init'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

I would apreciate all possible help in this, the #undef main at the start is because it won't let me run it otherwise. If it isn't there it gives me a "undefined reference to winmain@16" While I am creating a console application.

Splendid answered 9/5, 2015 at 23:53 Comment(0)
E
9

According to the include and library search paths ...\SDL2-2.0.3\x86_64-w64-mingw32\..., you're trying to build with a 64-bit SDL2. Judging from the compiler name mingw32-g++, I'd say you're using the mingw.org toolchain According to the Code::Blocks download page and my inspection of the contents of codeblocks-13.12mingw-setup.exe, the included toolchain is 32-bit only and can't create 64-bit binaries nor use 64-bit libraries.

If you want to use a pre-built SDL2, you either need to download the matching toolchain (64-bit mingw-w64) and use that, or change your build parameters to use the 32-bit build of SDL2 (it's present in the development libraries archive in the i686-w64-mingw32 directory).

Ehudd answered 11/5, 2015 at 1:43 Comment(0)
S
5

The following worked for me after some hard tinkering:

g++ -std=c++17 Test.cpp -I"include" -L"lib" -Wall -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -o Test

Some things you might not have realized that are important:

  • The "-l" flags need to go at the end.
  • The "-lmingw32" flag needs to be on there even when using the mingw-w64 64-bit compiler. Know that I don't understand why, but that I tried it and it doesn't work without it.
  • Your main function must be int main(int argc, char* argv[]) { because SDL needs to find it to take over.

My file structure looks like this:

Test/
  include/
    SDL.h
    SDL_main.h
    SDL_image.h
    ...
  lib/
    libSDL2.a
    libSDL2.dll.a
    libSDL2_image.a
    ...
  licenses/
  Test.exe
  SDL2.dll
  SDL2_image.dll
  zlib1.dll
  Test.cpp
  sdl2-config

Note that I have my command line's current directory at Test.
Also note that I have the mingw-w64 g++ executable's folder on the environment variables path.
Also also note that the ...s in the structure above mean there are more that should be in there and they come from the x86_64 folder given to you by SDL.

Silique answered 6/11, 2018 at 20:18 Comment(1)
I've been trying to fix this issue for the past 5 hours. I just copied your project structure to fit mine... lo and behold, it worked. Thank you :))Conall
K
5

in terminal or command prompt use bellow code

gcc main.c -lSDL2 -lSDL2main -o main

"main.c" is name of your file

Krell answered 24/9, 2021 at 15:21 Comment(1)
This quite easy fix works for a lot of errors alike this one, thank youWestney
W
2

SDL2 for MinGW ships as two sets of libraries: i686-w64-mingw32 (32-bit) and x86_64-w64-mingw32 (64-bit).

When -l... flags mysteriously don't fix the undefined reference errors, you're probably using the wrong set of files, not matching your compiler (32-bit files with 64-bit compiler, or vice versa).


Also #undef main should never be necessary if you do everything correctly.

In this case, changing int main() to int main(int, char **) (which is a SDL2 quirk) should let you remove it.

Whimsical answered 11/5, 2015 at 20:16 Comment(0)
J
0

this worked for me ....go to settings->compiler->linker settings->in other linker option box paste the following -lmingw32 -lSDL2main -lSDL2

Jollity answered 28/6, 2020 at 16:33 Comment(1)
please make sure to put code / commands in your answers in code fencesSuppliant
S
0

I had the same issue. You just need to swap the files with files from the other folder (x86_64-w64-mingw32 to i686-w64-mingw32)

Sammy answered 10/12, 2023 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.