SDL 2.0.20 Error with codeblocks

Hi, I am learning SDL, I got the following error when I building the project

codeblocks-mingw/current/MinGW/bin/…/lib/gcc/x86_64-w64-mingw32/8.1.0/…/…/…/…/x86_64-w64-mingw32/lib/…/lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain’
collect2.exe: error: ld returned 1 exit status

No problem with 2.0.14, any idea?

Edit: I don’t know but after comment this line in SDL_main.h the program can be build successfully
//#define main SDL_main

This is the source code that I am learning:

#include <stdbool.h>
#include <SDL.h>

bool is_running = false;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;

bool initialize_window(void) {
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0 ) {
        fprintf(stderr, "Error initializing SDL.\n");
        return false;
    }

    // Create a SDL Window
    window = SDL_CreateWindow(
        NULL,
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        800,
        600,
        SDL_WINDOW_BORDERLESS
    );
    if (!window) {
        fprintf(stderr, "Error creating SDL window.\n");
        return false;
    }

    // Create a SDL renderer
    renderer = SDL_CreateRenderer(window, -1, 0);
    if (!renderer) {
        fprintf(stderr, "Error creating SDL renderer.\n");
        return false;
    }

    return true;
}

void setup(void) {

}

void process_input(void) {
    SDL_Event event;
    SDL_PollEvent(&event);

    switch (event.type) {
    case SDL_QUIT:
        is_running = false;
        break;
    case SDL_KEYDOWN:
        if (event.key.keysym.sym == SDLK_ESCAPE)
            is_running = false;
        break;
    }
}

void update(void) {

}

void render(void) {
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    SDL_RenderClear(renderer);

    SDL_RenderPresent(renderer);
}

int main(int argc, char **argv) {

    is_running = initialize_window();

    setup();

    while (is_running) {
        process_input();
        update();
        render();
    }

    return 0;
}

Hi,

I can’t be 100% sure if that’s you’re exact problem since I can’t see your entire compile command. But this is a pretty common issue, so here are a few things:

  • When targeting Windows, SDL_main.h renames your main function to SDL_main, expecting you to link SDL2main.
  • Using mingw specifically, you need to link to mingw32 and pass the -mwindows argument to the linker.

Considering your error, you’re most likely missing the linker flag -mwindows. If it is there, you may want to ensure that you’re not missing a library (they should be mingw32, SDL2main and SDL2, in this order).

Hope that helps!

Hi,

After changing the linker flags to “-mwindows -lmingw32 -lSDL2main -lSDL2”, I can build the project with 0 error. Many thanks!