1

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)

karthikkk
  • 29
  • 1
  • 7
  • You forgot to add `-lSDL2main` to the linker flags, see the linked thread for details. Even if using `WinMain` works, it's not the intended solution. – HolyBlackCat Apr 24 '21 at 07:34

1 Answers1

0

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)
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Thanks a lot, It works. But why did it not work with the normal main() function? – karthikkk Apr 24 '21 at 05:06
  • 1
    @karthikkk - I'm not sure what your build environment is, but you are apparently building as Win32 GUI app, which is probably what you want instead of a console app. – selbie Apr 24 '21 at 05:14
  • It's not about a build environment. MinGW will (IIRC) accept both `main` and `WinMain` without any special flags. The problem is caused by `#define main SDL_main` in SDL headers, which is there because it wants to inject some custom initialization before the actual `main`. It malfunctioned in OP's case because they failed to provide some of the required linker flags. – HolyBlackCat Apr 24 '21 at 07:50
  • So when he executes his program, what is the entry point? The `WinMain` that invokes his `SDL_Init` or some prebuilt `main` provided by SDL? – selbie Apr 24 '21 at 16:42
  • OP should have an `SDL_main` (aka `main` because of the macro), which will be called by the actual `WinMain()`/`main` (not sure which one it is) provided by SDL. – HolyBlackCat Apr 24 '21 at 21:51