haskell-game / sdl2

Haskell bindings to the SDL2 library
Other
360 stars 86 forks source link

SDL2 can't be built on Windows at the moment #277

Open kapkekes opened 1 year ago

kapkekes commented 1 year ago

Problem

Can't build SDL2.

Description

I tried to build this library using Haskell Stack on Windows 11, but encountered a problem:

sdl2> C://Users//vashu//AppData//Local//Programs//stack//x86_64-windows//ghc-9.2.5//mingw//bin/ld.exe: C:\Users\vashu\AppData\Local\Programs\stack\x86_64-windows\msys2-20220904\mingw64\lib/libSDL2main.a(SDL_windows_main.c.obj):(.rdata$.refptr.__stack_chk_guard[.refptr.__stack_chk_guard]+0x0): undefined reference to `__stack_chk_guard'
sdl2> collect2.exe: error: ld returned 1 exit status

(full logs are here: log.txt)

As far as I can tell, this should be a problem with the SDL2 from MinGW64 repository. Sadly, I don't know how to contact its maintainers.

Steps to reproduce:

1) Install Stack: https://docs.haskellstack.org/en/stable/#how-to-install-stack 2) Execute:

stack exec -- pacman -Syu
stack exec -- pacman -S msys2-keyring
stack exec -- pacman -S mingw64-w64-x86_64-pkg-config mingw64-w64-x86_64-SDL2

3) Try to install SDL2:

stack install sdl2

Workaround solution

idea by @toolcreator: https://github.com/haskell-game/sdl2/issues/277#issuecomment-1339145721

the newest working version found by @jship: https://github.com/haskell-game/sdl2/issues/277#issuecomment-1784282404

kapkekes commented 1 year ago

Moreover, I forked this repository and ran the build workflow: it crashed on Windows.

https://github.com/kapkekes/sdl2/actions/runs/3497213769

UPD: logs of workflow expire fast, so you can just check the last run from here: https://github.com/haskell-game/sdl2/actions

toolcreator commented 1 year ago

I have the same issue and found a workaround. Instead of installing the most recent version of SDL2 from MSYS, use an older one:

$ stack exec -- curl -O https://repo.msys2.org/mingw/x86_64/mingw-w64-x86_64-SDL2-2.0.14-2-any.pkg.tar.zst
$ stack exec -- pacman -U mingw-w64-x86_64-SDL2-2.0.14-2-any.pkg.tar.zst

This is just the oldest version that's still available on repo.msys2.org, I didn't test any other.

Mikolaj commented 1 year ago

I have probably the same problem without stack, just with (an old) cabal: https://ci.appveyor.com/project/Mikolaj/lambdahack/build/job/5sab5gwkru56fpip

It says

* Missing (or bad) C libraries: SDL2, mingw32, SDL2main, SDL2
Mikolaj commented 1 year ago

And the workaround did not succeed for me, because my problem (the "Missing" line above) is the same as of https://github.com/haskell-game/sdl2/issues/277#issuecomment-1319848064 and is different than the one from the description of the ticket.

Mikolaj commented 1 year ago

And the same problem (mine) shows up in the official CI of this repo as well: https://github.com/haskell-game/sdl2/actions/runs/3732668333/jobs/6332423036#step:9:21

Mikolaj commented 1 year ago

I've opened a new ticket for that separate issue: https://github.com/haskell-game/sdl2/issues/280

SturdyPose commented 1 year ago

I've tried building with cabal and I've had success. So my workaround what @Mikolaj mentioned and using the latest SDL2 with libs on Windows machine (win11): Go to SDL2 releases github page and download latest SDL2 mingw file.

Unpack it and drop files to your msys mingw64 installation folder (example: C:\msys64\mingw64). Open msys and pacman -Syu and pacman -S pkgconf Append this to your .bashrc -> export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/mingw64/lib/pkgconfig

So there's this bug in cabal which can't see if pkg-config path is relative or absolute and some other issues with including files and lib folders (I'm not sure if stack has this same issue) but to workaround it, I had to go C:\<pathToMsys>\mingw64\lib\pkgconfig\sdl2.pc and edit prefix to: prefix=C:\\<pathToMsys>\\mingw64. Go to your global cabal config (/cabal/config) and add extra include dirs should look something like this:

extra-include-dirs: 
  C:\<pathToMsys>\mingw64\include,
  C:\<pathToMsys>\mingw64\include\SDL2

If you want SDL_image library, then you need to also copy mingw package to msys/mingw64 and edit sdl2_image.pc file with prefix.

I've spent whole week figuring this out. Hope this helps someone.

madjestic commented 1 year ago

I would like to expand a bit on @Poselsky answer:

Unpack it and drop files to your msys mingw64 installation folder (example: C:\msys64\mingw64).

after extracting the SDL2 archive, the files in questions, depending on your setup, may be here: C:\Users\username\Downloads\SDL2-devel-2.26.4-mingw\SDL2-2.26.4\x86_64-w64-mingw32

\bin
\include
\lib
\share

You need to copy the dirs over to mingw64 matching dirs, e.g. C:\ghcup\msys64\mingw64:

C:\ghcup\msys64\mingw64\bin
C:\ghcup\msys64\mingw64\incude
...

Respectively, edit the cabal/config file

...
-- flags:
extra-include-dirs: C:\ghcup\msys64\mingw64\include,
                    C:\ghcup\msys64\mingw64\include\SDL2
-- deterministic:
...

I did not have to add the path to the .bashrc

Otherwise it works, thanks!

Lev135 commented 1 year ago

I have the same issue and found a workaround. Instead of installing the most recent version of SDL2 from MSYS, use an older one:

$ stack exec -- curl -O https://repo.msys2.org/mingw/x86_64/mingw-w64-x86_64-SDL2-2.0.14-2-any.pkg.tar.zst
$ stack exec -- pacman -U mingw-w64-x86_64-SDL2-2.0.14-2-any.pkg.tar.zst

This is just the oldest version that's still available on repo.msys2.org, I didn't test any other.

This failed for sdl2-2.5.5.0 with the following errors:

``` error: In file included from dist\build\SDL\Raw\Types_hsc_make.c:1: Types.hsc: In function 'SDL_main': Types.hsc:1411:15: error: 'SDL_Vertex' undeclared (first use in this function); did you mean 'SDL_mutex'? C:\ghcup\ghc\8.10.7/lib/template-hsc.h:96:39: note: in definition of macro 'hsc_size' 96 | hsc_printf("(%ld)", (long) sizeof(t)); | ^ Types.hsc:1411:15: note: each undeclared identifier is reported only once for each function it appears in C:\ghcup\ghc\8.10.7/lib/template-hsc.h:96:39: note: in definition of macro 'hsc_size' 96 | hsc_printf("(%ld)", (long) sizeof(t)); | ^ Types.hsc:1412:20: error: expected specifier-qualifier-list before 'SDL_Vertex' C:\ghcup\ghc\8.10.7/lib/template-hsc.h:102:7: note: in definition of macro 'hsc_alignment' 102 | x b; \ | ^ In file included from C:/ghcup/ghc/8.10.7/mingw/lib/gcc/x86_64-w64-mingw32/9.2.0/include/stddef.h:1, from C:/ghcup/ghc/8.10.7/mingw/x86_64-w64-mingw32/include/inttypes.h:14, from C:/Users/lev_1/AppData/Local/Programs/stack/x86_64-windows/msys2-20210604/mingw64/include/SDL2/SDL_stdinc.h:75, from C:/Users/lev_1/AppData/Local/Programs/stack/x86_64-windows/msys2-20210604/mingw64/include/SDL2/SDL.h:33, from Types.hsc:83: C:\ghcup\ghc\8.10.7/lib/template-hsc.h:104:38: error: 'struct __anon_x__' has no member named 'b' 104 | hsc_printf("%lu", (unsigned long)offsetof(struct __anon_x__, b)); \ | ^~~~~~~~ ```

It seems that something has been changed through this versions. But everything is Ok with 2.0.22, i. e.

stack exec -- curl -O https://repo.msys2.org/mingw/x86_64/mingw-w64-x86_64-SDL2-2.0.22-2-any.pkg.tar.zst
stack exec -- pacman -U mingw-w64-x86_64-SDL2-2.0.22-2-any.pkg.tar.zst

Hope this will be useful as an temporary workaround.

madjestic commented 1 year ago

I have the same issue and found a workaround. Instead of installing the most recent version of SDL2 from MSYS, use an older one:

$ stack exec -- curl -O https://repo.msys2.org/mingw/x86_64/mingw-w64-x86_64-SDL2-2.0.14-2-any.pkg.tar.zst
$ stack exec -- pacman -U mingw-w64-x86_64-SDL2-2.0.14-2-any.pkg.tar.zst

This is just the oldest version that's still available on repo.msys2.org, I didn't test any other.

This failed for sdl2-2.5.5.0 with the following errors:

``` error: In file included from dist\build\SDL\Raw\Types_hsc_make.c:1: Types.hsc: In function 'SDL_main': Types.hsc:1411:15: error: 'SDL_Vertex' undeclared (first use in this function); did you mean 'SDL_mutex'? C:\ghcup\ghc\8.10.7/lib/template-hsc.h:96:39: note: in definition of macro 'hsc_size' 96 | hsc_printf("(%ld)", (long) sizeof(t)); | ^ Types.hsc:1411:15: note: each undeclared identifier is reported only once for each function it appears in C:\ghcup\ghc\8.10.7/lib/template-hsc.h:96:39: note: in definition of macro 'hsc_size' 96 | hsc_printf("(%ld)", (long) sizeof(t)); | ^ Types.hsc:1412:20: error: expected specifier-qualifier-list before 'SDL_Vertex' C:\ghcup\ghc\8.10.7/lib/template-hsc.h:102:7: note: in definition of macro 'hsc_alignment' 102 | x b; \ | ^ In file included from C:/ghcup/ghc/8.10.7/mingw/lib/gcc/x86_64-w64-mingw32/9.2.0/include/stddef.h:1, from C:/ghcup/ghc/8.10.7/mingw/x86_64-w64-mingw32/include/inttypes.h:14, from C:/Users/lev_1/AppData/Local/Programs/stack/x86_64-windows/msys2-20210604/mingw64/include/SDL2/SDL_stdinc.h:75, from C:/Users/lev_1/AppData/Local/Programs/stack/x86_64-windows/msys2-20210604/mingw64/include/SDL2/SDL.h:33, from Types.hsc:83: C:\ghcup\ghc\8.10.7/lib/template-hsc.h:104:38: error: 'struct __anon_x__' has no member named 'b' 104 | hsc_printf("%lu", (unsigned long)offsetof(struct __anon_x__, b)); \ | ^~~~~~~~ ```

It seems that something has been changed through this versions. But everything is Ok with 2.0.22, i. e.

stack exec -- curl -O https://repo.msys2.org/mingw/x86_64/mingw-w64-x86_64-SDL2-2.0.22-2-any.pkg.tar.zst
stack exec -- pacman -U mingw-w64-x86_64-SDL2-2.0.22-2-any.pkg.tar.zst

Hope this will be useful as an temporary workaround.

It looks like you are using an older GHC version. If that's unintentional, try a more recent GHC version as well as a newer sdl2.

dpwiz commented 1 year ago

'SDL_Vertex' undeclared

This is a newer feature and provided under the recent-ish flags. If you really have to use the old SDL2 version, turn it off. (I.e. unrelated to windows build errors.)

SturdyPose commented 1 year ago

Those who still have compilation issues, take a look at this potential workaround. After trying to get stack working instead of cabal, I broke my previous cabal environment and couldn't compile SDL anymore. Build script with these environment vars fixed environment for me: https://github.com/haskell/cabal/issues/7096

This brings question, can't we supply a flag which would link against SDL2.dll? Looks like cabal itself has some linking issues and if this were to occur again after potential build bug fixes, temporary dll link seems like a good fix to me. Also novices who would love to dabble with SDL wouldn't need to learn intricacies of MINGW.

jship commented 10 months ago

I have the same issue and found a workaround. Instead of installing the most recent version of SDL2 from MSYS, use an older one:

$ stack exec -- curl -O https://repo.msys2.org/mingw/x86_64/mingw-w64-x86_64-SDL2-2.0.14-2-any.pkg.tar.zst
$ stack exec -- pacman -U mingw-w64-x86_64-SDL2-2.0.14-2-any.pkg.tar.zst

This is just the oldest version that's still available on repo.msys2.org, I didn't test any other.

The latest version I had success using is v2.24.1-1:

stack exec -- curl -O  https://repo.msys2.org/mingw/x86_64/mingw-w64-x86_64-SDL2-2.24.1-1-any.pkg.tar.zst
stack exec -- pacman -U .\mingw-w64-x86_64-SDL2-2.24.1-1-any.pkg.tar.zst

Everything after that version failed with the __stack_chk_guard issue. I haven't tried @Poselsky's approach of manually installing the newest, but the data point of v2.24.1-1 being good may be helpful for others. v2.24.1-1 is from 14-Oct-2022 according to the msys2 repo.

tmheath commented 7 months ago

@jship @Poselsky @madjestic @Lev135 @Mikolaj @toolcreator @kapkekes I apologize if I'm pinging you without cause, I've referenced this problem with the package maintainers at the below link. I don't know enough to really be helpful, one of you probably are better suited to help figure out the real problem. https://github.com/libsdl-org/SDL/issues/9058

Sofviic commented 4 months ago

Just here to confirm that this is still an issue; and jship's solution still works. Didn't work with mingw-w64-x86_64-SDL2-2.30.2-1 but did work with mingw-w64-x86_64-SDL2-2.24.1-1.

Other stuff I did before that, that didn't work:

Edit (2024.06.14): still true as of June 2024, mingw-w64-x86_64-SDL2-2.24.1-1 works, & mingw-w64-x86_64-SDL2-2.30.3-1 doesn't.

ProgrammingIncluded commented 1 month ago

It looks like the suggestions from https://github.com/haskell-game/sdl2/issues/277#issuecomment-1784282404 and https://github.com/haskell-game/sdl2/issues/277#issuecomment-2071273743 no longer work as mingw-w64-x86_64-SDL2-2.24.1-1 has been removed from the archive in favor of a minor version update: mingw-w64-x86_64-SDL2-2.24.2-1

However, I get the same error using this version:

Error with SDL2-2.24.2-1 ```bash 2024-08-11 17:39:47.796175: [warn] sdl2 > error: ld.lld: error: undefined symbol: __stack_chk_fail 2024-08-11 17:39:47.796175: [warn] sdl2 > >>> referenced by libSDL2main.a(SDL_windows_main.c.obj):(.text) 2024-08-11 17:39:47.796675: [warn] sdl2 > 2024-08-11 17:39:47.796675: [warn] sdl2 > ld.lld: error: undefined symbol: __stack_chk_guard 2024-08-11 17:39:47.796675: [warn] sdl2 > >>> referenced by libSDL2main.a(SDL_windows_main.c.obj):(.refptr.__stack_chk_guard) 2024-08-11 17:39:47.797175: [warn] sdl2 > clang: error: linker command failed with exit code 1 (use -v to see invocation) 2024-08-11 17:39:47.797175: [warn] sdl2 > ```

Any other ideas in terms of workarounds or have a copy of the original build? Thanks.

ProgrammingIncluded commented 1 month ago

I was able to figure out a workaround, it takes inspiration from @SturdyPose suggestion, however does not assume mingw64 installation on host by setting everything up via Bash.

For our example, I will be using 2.30.6 variant of SDL2.

  1. Grab a build from the official SDL2 Github. in this case, SDL2-devel-2.30.6-mingw.zip or the tar.gz equivalent.

For our tutorial I will assume you unpack the zip file in your project directory but this file is going to be used globally by all projects that rely on SDL2 unless you redo the steps.

  1. In the zip file, you should see a x86_64-w64-mingw32/ inside with the appropriate folders:
bin/
include/
lib/
share/
  1. In your project folder, make sure you already have sdl2 library added in the package.yaml via dependencies.
  2. Type in the following:
stack exec -- bash

This should start an interactive shell with MingW64 (or your default MingW setup with Stack.)

  1. Then you can run the following in the interactive shell:
 cp SDL2-2.30.6/x86_64-w64-mingw32/lib/* -r /mingw64/lib/
 cp SDL2-2.30.6/x86_64-w64-mingw32/include/* -r /mingw64/include/
 cp SDL2-2.30.6/x86_64-w64-mingw32/bin/* -r /mingw64/bin/

Once you are done, you can safely exit the shell.

Voila! You now should be able to build. This workaround assumes you only have one version of SDL installed at a time and injects SDL into your /mingw64 file path that stack uses under-the-hood.

If you plan to change the version of SDL2, make sure to force a rebuild by removing sdl2 Haskell library via:

stack exec -- ghc-pkg unregister --force sdl2