2

Trying to write a C program using the SDL2 library on Windows 11 - zero compiler errors, but still cannot execute the compiled program.

On my system I've installed the MINGW32 GCC compiler, compiling and running a "hello world" is no problem. The SDL2 version I downloaded is the mingw32-devel variant.

In my SDL2 project I've copied over the include and lib folder from the i686 version of SDL2, as well as the respective .dll file. Project structure:

.
| src
  | include
  | lib
| SDL2.dll
| Makefile
| sdl_test.c

The .c file compiles fine on Linux. It only opens a window and waits for a quit signal.

As you can see below, compiling produces no warnings or errors on Windows, but fails to run:

PS C:\Users\eiriken\Documents\sdl-test> make     
gcc -Wall -Isrc/include -Lsrc/lib sdl-test.c -lmingw32 -lSDL2main -lSDL2 -o out.exe
PS C:\Users\eiriken\Documents\sdl-test> .\out.exe
Program 'out.exe' failed to run: The specified executable is not a valid application for this OS platform.At line:1 char:1
+ .\out.exe
+ ~~~~~~~~~.
At line:1 char:1
+ .\out.exe
+ ~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

What am I doing wrong, except for trying to develop code on a Windows platform?

YSC
  • 38,212
  • 9
  • 96
  • 149
  • Does SDL2 work with the free Microsoft [Visual Studio](https://visualstudio.microsoft.com/vs/community/) 2022 community edition? – Eljay Oct 10 '22 at 11:45
  • 3
    _What am I doing wrong, except for trying to develop code on a Windows platform?_ you made my day :) try to invert the order: `-lSDL2 -lSDL2main` instead of `-lSDL2main -lSDL2` – David Ranieri Oct 10 '22 at 11:50
  • 2
    @DavidRanieri `-lSDL2main -lSDL2` is the correct order. Messing up the order causes a link error. – HolyBlackCat Oct 10 '22 at 12:01
  • 1
    OP, What is your SDL2 verson? In 2.24.1 they supposedly fixed the MinGW issues. Also this may or may not be fixed in a newer MinGW (you can try getting it from [MSYS2](https://stackoverflow.com/q/30069830/2752075)). – HolyBlackCat Oct 10 '22 at 12:02
  • Are you running an x64 build of Windows 11 without WoW installed? – Dai Oct 10 '22 at 12:31
  • Please post your `makefile` - ideally post your `sdl_test.c` too. – Dai Oct 10 '22 at 12:32
  • @HolyBlackCat you're a wizard - that release came only six days ago. Upgrading from 2.24.0 worked wonderfully, thank you. – Eirik Husebø Oct 10 '22 at 12:49
  • @HolyBlackCat Can you explain why ordering matters w.r.t. the linker? And why is the end-result an invalid executable despite no compiler/linker warnings? This is weird... – Dai Oct 10 '22 at 15:22
  • @Dai Because the first library (which is a static library) calls functions from the second. The invalid executable is unrelated to the order. I think it's either the outdated toolchain, or plain broken binaries. – HolyBlackCat Oct 10 '22 at 15:58

1 Answers1

0

Try to copy the SDL2.dll file into the same directory as the executable file.

Nicolai Nita
  • 173
  • 9