0

I know this has probably been asked/answered a thousand times across the internet but I can't seem to find any of the answers that will finally find where I'm going wrong.

I'm currently following a course on Udemy on "C++ For Complete Beginners" and I've reached almost the very end of the course where we start to use SDL for a small project.

When I follow the exact steps that the tutor provides I get an error.

The error in question looks like this:

C:/Users/Ghost/gcc/x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text+0x3d): undefined reference to `WinMain'

And the code to go alongside it is:

#include <iostream>
#include <SDL.h>
using namespace std;

int main() {
    if(SDL_Init(SDL_INIT_VIDEO) < 0 ) {
        cout << "SDL Init Failed" << endl;
        return 1;
    }

    cout << "SDL Init Succeeded" << endl;

    SDL_Quit();

    return 0;
}

All that the program needs to do is either output one of the cout statements. Nothing more, Nothing less.

I have installed SDL2, mingw and eclipse. Eclipse has worked with everything up until this point but when I followed the steps by the tutor it no longer wants to work.

The header files and the library has been connected to the program as well.

Ghost
  • 1
  • You forgot to link against `-lSDL2main` (you need following linker flags, in this order: `-lmingw32 -lSDL2main -lSDL2`) **and/or** you're using SDL built for x32 with an x64 compiler. – HolyBlackCat Jun 19 '20 at 14:50
  • @FrançoisAndrieux I kind of disagree with the closure. After the lengthy explanation of what exactly `WinMain` is, the top answer just says *"you probably forgot `main`"*, which isn't very helpful in case of SDL2 with its `#define main SDL_main`. – HolyBlackCat Jun 19 '20 at 14:52
  • From what I've looked at I'm using a x64 compiler and the x64 part of SDL The version of SDL that I am using is the 2.0.12 variant if that makes any difference – Ghost Jun 19 '20 at 14:55
  • No, the exact SDL2 version doesn't matter. If you're sure you got the right prebuilt SDL (x64, matching your compiler), then the problem is with the linker flags, as I said. If you're new to Eclipse (don't know where to set the flags), consider compiling it from the terminal first. – HolyBlackCat Jun 19 '20 at 14:57
  • I've found where the linker flags go and have added them to the program. Now I'm getting a different error, this time regarding "SDL_main" So progress is being made – Ghost Jun 19 '20 at 15:05
  • 2
    SDL2 requires `main` to be `int main(int, char **)` rather than `int main()`. (Or equivalent: parameters could be named, and/or the second parameter could be `char *[]`.) – HolyBlackCat Jun 19 '20 at 15:06
  • YES! Haha! It works! Thank you very much for the help! Now I can continue with the course. – Ghost Jun 19 '20 at 15:09

0 Answers0