c++gccmingwsdl-2codelite

GCC C++ linking error : Undefined reference to 'WinMain@16'


I'm have linking issue's on codelite when I try to build my project. I get the error 'undefined reference to 'WinMain@16'.

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

int main()
{
    
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Quit();
        
    return 0;
}

This is my main file, I am trying to use the SDL library for a project I am working on. Initially, I used to get the error 'Undefined reference to SDL_Init()'. I then included the SDL2 in the projects linker settings. Now I get the same error for WinMain@16.

Is this is an issue with linking? am I supposed to include a library in my project settings for removing this error ?.

this is the information about my MinGW.

Using built-in specs.
COLLECT_GCC=D:\Min_GW\bin\gcc.exe
COLLECT_LTO_WRAPPER=d:/min_gw/bin/../libexec/gcc/mingw32/9.2.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-9.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-static --enable-shared --enable-threads --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --enable-libgomp --disable-libvtv --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --disable-build-format-warnings --prefix=/mingw --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --enable-nls --with-pkgversion='MinGW.org GCC Build-2'
Thread model: win32
gcc version 9.2.0 (MinGW.org GCC Build-2)


Solution

  • Rename your main to be WinMain with the following parameters

    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        PSTR lpCmdLine, INT nCmdShow)
    {
        SDL_Init(SDL_INIT_EVERYTHING);
        SDL_Quit();
        
        return 0;
    }
    

    Most of those parameters will require #include <windows.h> If that header is not available, then declare as:

    int __stdcall WinMain(void*, void*, char*, int)