Search code examples
c++mingwsdlsdl-3

cannot find -lSDL3main: No such file or directory


I recently installed SDL3. I wanted to make an easy test file just to get familiar with SDL, but it doesn't work.

To compile my code I use:

g++ test.cpp -lmingw32 -lSDL3main -lSDL3 -Lsrc/lib

...but I get an error regarding -lSDL3main:

...x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL3main: No such file or directory

So I thought why don't I just delete the flag if it doesn't find it, and so I did, but after that I still got an errror message but this time it was different:

.../M/B/src/mingw-w64/mingw-w64-crt/crt/crtexewin.c:67:(.text.startup+0xc5): undefined reference to 'WinMain'

For Source Code I just used the demo from the SDL3 wiki:

#include <SDL3/SDL.h>
#include <iostream>

int main(int argc, char**argv) {
    SDL_Window *window;                    

    SDL_Init(SDL_INIT_VIDEO);              

    window = SDL_CreateWindow(
        "An SDL3 window",                  // window title
        640,                               // width, in pixels
        480,                               // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
    );

      if (window == NULL) {
          SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not create window: %s\n", SDL_GetError());
        return 1;
    }

    // The window is open: could enter program loop here (see SDL_PollEvent())

    SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example

    SDL_DestroyWindow(window);

    SDL_Quit();
    return 0;
}

Solution

  • In SDL3 you don't need to link SDLmain (it doesn't exist anymore). Instead add the following in the top of the file containing your main function.

    #include <SDL3/SDL_main.h>
    

    quoting the docs SDL3 main-function:

    Previous versions of SDL had a static library, SDLmain, that you would link your app against. SDL3 still has the same macro tricks, but the static library is gone. Now it's supplied by a "single-header library," which means you #include <SDL3/SDL_main.h>

    main needs to have the following signature

    #include <SDL3/SDL.h>
    #include <SDL3/SDL_main.h>
    
    int main(int argc, char **argv)
    {
      // code goes here
      return 0; // not optional
    }
    

    another better option is to use SDL_MAIN_USE_CALLBACKS, and not have a main anymore and instead have a few callbacks.

    #define SDL_MAIN_USE_CALLBACKS 1
    #include <SDL3/SDL.h>
    #include <SDL3/SDL_main.h>
    
    SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
        // initialize application here and fill appstate with your state
    
        return SDL_APP_CONTINUE;
    }
    
    SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
        // handle event here
    
        return SDL_APP_CONTINUE;
    }
    
    SDL_AppResult SDL_AppIterate(void* appstate) {
        // draw graphics and game Update here
    
        return SDL_APP_CONTINUE;
    }
    
    void SDL_AppQuit(void* appstate, SDL_AppResult result) {
        // cleanup here
    }
    

    some advantages of this so far are:

    1. works on all platforms with no modifications (especially emscripten)
    2. screen redraws while resizing on most platforms (especially windows)
    3. automatic framerate control with SDL_HINT_MAIN_CALLBACK_RATE