0

I am trying to make a simple window with SDL2 in VSCode. I just can't seem to find a working solution anywhere. Every time I run my makefile or just run the command, I get the error"undefined reference to `WinMain@16'". I can't figure out why. My mingw is working fine as I can compile other files with no problem.

My program is this:

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

const int WIDTH = 600;
const int HEIGHT = 600;

int main(int argc, char *argv[]){
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window *window = SDL_CreateWindow("SDL TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);

    if (window == NULL){
        std::cout << "SDL WINDOW ERROR: " << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_Event windowEvent;
    bool running = true;

    while (running){
        if (SDL_PollEvent(&windowEvent)){
            if (windowEvent.type == SDL_QUIT){
                running = false;
            }
        }
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

My makefile is:

all:
    g++ -I src/include -L src/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2

I have my main function and everything is compiled fine I think. Any help would be greatly appreciated!

  • Pass `-mconsole` see https://gcc.gnu.org/onlinedocs/gcc/x86-Windows-Options.html . `g++`is trying to link a Windows application. If this is what you want then replace 'main` with `WinMain` see here for the full signature https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-winmain – Richard Critten Mar 19 '23 at 00:55
  • I didn't have time to comment on your other post before it was deleted. Your "undefined reference"s are most likely caused by trying to use x32 .a files (libSDL2.a and others) with x64 compiler, or vice versa. See [How do I use SDL2 in my programs correctly?](https://stackoverflow.com/q/64396979/2752075) for more guidance. Also, consider [using MSYS2](https://stackoverflow.com/q/30069830/2752075) to install both GCC and SDL2. It will give you access to the latest compiler (yours is outdated, 9.x vs 12.x), and to a version of SDL2 that's certainly compatible. – HolyBlackCat Mar 19 '23 at 15:53
  • @RichardCritten This is wrong. `-mconsole` is the default. MinGW will always accept either `main` or `WinMain` regardless of this flag, the error message is misleading. – HolyBlackCat Mar 19 '23 at 15:56
  • Thank you so much it finally works now after I got it installed and setup! – Pygameuserr Mar 19 '23 at 17:11

0 Answers0