Problems while compiling with mingw

On Fedora (GNU/Linux), I can compile the program using:

cc -O3 -lSDL2 -lSDL2_mixer -lSDL2_image -Wall -Werror -pedantic src/*.c -o pong

I (roughly) followed this guide: Lazy Foo' Productions - Setting up SDL 2 on MinGW
And I tried compiling it with (using mysys2):
cc src/*.c -Ii686-w64-mingw32\include -Li686-w64-mingw32\lib -lmingw32 -lSDL2 -lSDL2main -lSDL2_mixer -lSDL2_image -o pong.exe

But I get this error: In file included from i686-w64-mingw32\include/SDL2/SDL_audio.h:37, from i686-w64-mingw32\include/SDL2/SDL.h:36, from src/screenshot.c:3: src/screenshot.c: In function 'pong_Screenshot': i686-w64-mingw32\include/SDL2/SDL_thread.h:120:25: error: '_beginthreadex' undeclared (first use in this function); did you mean 'SDL_beginthread'? 120 | #define SDL_beginthread _beginthreadex | ^~~~~~~~~~~~~~ i686-w64-mingw32\include/SDL2/SDL_thread.h:144:102: note: in expansion of macro 'SDL_beginthread' 144 | #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread) | ^~~~~~~~~~~~~~~ src/screenshot.c:33:30: note: in expansion of macro 'SDL_CreateThread' 33 | SDL_Thread *thread = SDL_CreateThread(pong_SaveFile, "SaveFile", (void *) screenshot); | ^~~~~~~~~~~~~~~~ i686-w64-mingw32\include/SDL2/SDL_thread.h:120:25: note: each undeclared identifier is reported only once for each function it appears in 120 | #define SDL_beginthread _beginthreadex | ^~~~~~~~~~~~~~ i686-w64-mingw32\include/SDL2/SDL_thread.h:144:102: note: in expansion of macro 'SDL_beginthread' 144 | #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread) | ^~~~~~~~~~~~~~~ src/screenshot.c:33:30: note: in expansion of macro 'SDL_CreateThread' 33 | SDL_Thread *thread = SDL_CreateThread(pong_SaveFile, "SaveFile", (void *) screenshot); | ^~~~~~~~~~~~~~~~ i686-w64-mingw32\include/SDL2/SDL_thread.h:123:23: error: '_endthreadex' undeclared (first use in this function) 123 | #define SDL_endthread _endthreadex | ^~~~~~~~~~~~ i686-w64-mingw32\include/SDL2/SDL_thread.h:144:144: note: in expansion of macro 'SDL_endthread' 144 | #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread) | ^~~~~~~~~~~~~ src/screenshot.c:33:30: note: in expansion of macro 'SDL_CreateThread' 33 | SDL_Thread *thread = SDL_CreateThread(pong_SaveFile, "SaveFile", (void *) screenshot); | ^~~~~~~~~~~~~~~~
At first I used the ā€˜includeā€™ and ā€˜libā€™ folder from ā€œx86_64-w64-mingw32ā€, then replaced it with the folders from ā€œi686-w64-mingw32ā€, and I still get the same error. The error code says that ā€˜_beginthreadā€™ is undeclared, while looking into ā€˜SDL_thread.hā€™ it seems that it should be defined under Windows 7:

#if defined(__WIN32__)
#include <process.h> /* _beginthreadex() and _endthreadex() */
#endif
#if defined(__OS2__) /* for _beginthread() and _endthread() */
#ifndef __EMX__
#include <process.h>
#else
#include <stdlib.h>
#endif
#endif

I have -mthreads -mwindows in my MinGW linker command, so that might be worth trying.

IIRC the link order for those three libs must be exactly -lmingw32 -lSDL2main -lSDL2 and yes, -mwindows is also needed somewhere (donā€™t remember anything about -mthreads, but sounds plausible that it should be used)

cc src/*.c -Ii686-w64-mingw32\include -Li686-w64-mingw32\lib -mwindows -mthreads -lmingw32 -lSDL2main -lSDL2 -lSDL2_mixer -lSDL2_image -o pong.exe

Still getting the same error. I tried moving -mwindows -mthreads in different places, but it still wonā€™t work. Even with -lmingw32 -lSDL2main -lSDL2 in the right order, it still doesnā€™t work.

Also while searching for a solution, Iā€™ve found this: https://github.com/ihhub/fheroes2/issues/1646

Hi @hgdagon, we dropped MinGW support in favor of Visual Studio long time ago as this is not a common platform for development.

And this Installation - SDL Wiki

As of SDL 2.0.3, the codebase still compiles on Cygwin and MingW32, but we expect these to stop working in the future. MingW64 is still supported (and despite the name, can also build 32-bit binaries).

So Mingw probably isnā€™t supported anymore. Later Iā€™ll try installing Visual Studio and Iā€™ll compile my project using that.

You missed the secend sentence of the quote you posted from the Wiki:

MingW64 is still supported (and despite the name, can also build 32-bit binaries).

-Li686-w64-mingw32 from you comandline suggests that youā€™re indeed using mingw-w64 (see the w64 part of it) so thatā€™s not it.

Furthermore, when looking more closely at your compiler error (itā€™s hard to read as itā€™s missing newlines), itā€™s not a linker error, but a compiler error, so the order of the libs doesnā€™t cause this.

Not s sure what does, but #if defined(__WIN32__) looks a bit suspicious, I think the more common define is _WIN32, so it might be worth trying to adjust that line SDL_thread.h to use that instead

I edited the line in ā€˜SDL_thread.hā€™ to #if defined(_WIN32) and I still get the same errors.
And hereā€™s the error text with better formatting: In file included from i686-w64-mingw32\include/SDL2/SDL_audio.h:37, - Pastebin.com

I use MinGW-w64 for both 32-bit and 64-bit builds of my app, and have never had any problems. Where did you get MinGW from? I think mine came from winlibs.com, with the MSVCRT runtime library,

1 Like

Thanks! This fixed my problem. I downloaded and extracted the winlibs .zip to ā€˜D:\mingw64ā€™, then I compiled my program using this command:

"D:\mingw64\bin\gcc.exe" src/*.c -Ix86_64-w64-mingw32\include -Lx86_64-w64-mingw32\lib -w -Wl,-subsystem,windows -mwindows -mthreads -lmingw32 -lSDL2main -lSDL2 -lSDL2_mixer -lSDL2_image -o pong.exe

Iā€™m not sure why Mysys2 was giving me problems.

1 Like