Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to SDL_Init

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.

like image 597
miquel09 Avatar asked May 09 '15 23:05

miquel09


4 Answers

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).

like image 108
David Macek Avatar answered Nov 15 '22 01:11

David Macek


You have several errors here.

1.

Don't undef main just because you don't know why it's here! When you're using SDL, main() (almost) always must look like int main(int, char **) {}. So, remove #undef main and change int main() to int main(int, char **).

2.

You must use contents of i686-w64-mingw32 instead of x86_64-w64-mingw32 when building x32 executables. So, adjust your compilation flags properly.

x86_64-w64-mingw32 is probably for x64 applications, but I think you're building x32 one.

like image 22
HolyBlackCat Avatar answered Nov 15 '22 01:11

HolyBlackCat


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.

like image 29
Dustin Morrison Avatar answered Nov 14 '22 23:11

Dustin Morrison


in terminal or command prompt use bellow code

gcc main.c -lSDL2 -lSDL2main -o main

"main.c" is name of your file

like image 1
lion Avatar answered Nov 15 '22 01:11

lion