[SOLVED] GHC on Windows fails with "undefined reference to mingw_app_type", etc

I installed GHC on Windows 11 yesterday, via ghcup:

$ uname -a
MINGW64_NT-10.0-22621 couch-potato 3.4.9.x86_64 2023-09-15 12:15 UTC x86_64 Msys
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 9.2.8
$ cabal --version
cabal-install version 3.10.1.0
compiled using version 3.10.1.0 of the Cabal library
$ gcc --version
gcc.exe (Rev2, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Today, I tried using GHC. I tried building a simple ā€œHello, World!ā€ program (the default program created by cabal init). However, it fails with linker errors:

$ cabal-build
Resolving dependencies...
Build profile: -w ghc-9.2.8 -O1
In order, the following will be built (use -v for more details):
 - hello-world-0.1.0.0 (exe:hello-world) (first run)
Configuring executable 'hello-world' for hello-world-0.1.0.0..
Preprocessing executable 'hello-world' for hello-world-0.1.0.0..
Building executable 'hello-world' for hello-world-0.1.0.0..
[1 of 1] Compiling Main             ( src-exe\Main.hs, C:\\Users\ppelleti\prg\hs\hello\dist-newstyle\build\x86_64-windows\ghc-9.2.8\hello-world-0.1.0.0\x\hello-world\build\hello-world\hello-world-tmp\Main.o )
Linking C:\\Users\\ppelleti\\prg\\hs\\hello\\dist-newstyle\\build\\x86_64-windows\\ghc-9.2.8\\hello-world-0.1.0.0\\x\\hello-world\\build\\hello-world\\hello-world.exe ...
C://ghcup//ghc//9.2.8//mingw//bin/ld.exe: C://ghcup//ghc//9.2.8//mingw//bin/../x86_64-w64-mingw32/lib/crt2.o:crtexe.c:(.rdata$.refptr.mingw_app_type[.refptr.mingw_app_type]+0x0): undefined reference to `mingw_app_type'
C://ghcup//ghc//9.2.8//mingw//bin/ld.exe: C://ghcup//ghc//9.2.8//mingw//bin/../x86_64-w64-mingw32/lib/crt2.o:crtexe.c:(.rdata$.refptr.mingw_initcharmax[.refptr.mingw_initcharmax]+0x0): undefined reference to `mingw_initcharmax'
C://ghcup//ghc//9.2.8//mingw//bin/ld.exe: C://ghcup//ghc//9.2.8//mingw//bin/../x86_64-w64-mingw32/lib/crt2.o:crtexe.c:(.rdata$.refptr.mingw_initltssuo_force[.refptr.mingw_initltssuo_force]+0x0): undefined reference to `mingw_initltssuo_force'
C://ghcup//ghc//9.2.8//mingw//bin/ld.exe: C://ghcup//ghc//9.2.8//mingw//bin/../x86_64-w64-mingw32/lib/crt2.o:crtexe.c:(.rdata$.refptr.mingw_initltsdyn_force[.refptr.mingw_initltsdyn_force]+0x0): undefined reference to `mingw_initltsdyn_force'
C://ghcup//ghc//9.2.8//mingw//bin/ld.exe: C://ghcup//ghc//9.2.8//mingw//bin/../x86_64-w64-mingw32/lib/crt2.o:crtexe.c:(.rdata$.refptr.mingw_initltsdrot_force[.refptr.mingw_initltsdrot_force]+0x0): undefined reference to `mingw_initltsdrot_force'
collect2.exe: error: ld returned 1 exit status
`gcc.exe' failed in phase `Linker'. (Exit code: 1)
Error: cabal-3.10.1.0.exe: Failed to build exe:hello-world from
hello-world-0.1.0.0.

Any idea what causes these and how to fix them?

Searching online reveals that other people are also getting this error (not with Haskell, but with other languages like C++ and Rust). However, none of the pages Iā€™ve looked at so far give a clear explanation of whatā€™s wrong and how to fix it.

1 Like

This may not be of use to you, as it does not go directly to your question, and you may well want to stay with GHCup and Cabal (the tool), but as a Windows 11 user, I have had no problems with Stack and GHC 9.2.8. You can use Stack directly in PowerShell (you donā€™t need to use a MSYS2 environment).

To use GHC 9.2.8, you would specify resolver: lts-20.26 in Stackā€™s project-level configuration file (stack.yaml).

2 Likes

I have no particular attachment to GHC 9.2.8. Thatā€™s just what I got by following the installation instructions. (I think because 9.2.8 is marked as ā€œrecommendedā€.)

I wonder if a newer version of GHC would have any better chance of working than the ā€œrecommendedā€ version? Iā€™m attempting to install 9.6.3 now. (Although Iā€™m having trouble getting it to download successfully.)

Iā€™ve used stack in the past, but I got the impression itā€™s not being maintained as actively anymore. I might try it if I get too frustrated with ghcup. (Everyone says ghcup is the recommended way to install GHC now, so I thought I should try it. So far, Iā€™m not impressed.)

Iā€™m much more comfortable working in MSYS2/bash. I donā€™t know anything about PowerShell.

Edit: I succeeded in installing GHC 9.6.3, and it worked fine without any problems. So apparently ghcup ā€œrecommendsā€ a version of GHC that doesnā€™t work!

What msys2 version are you using?

How do I find that out?

I think you were experiencing this GHC bug #21111: [windows] GHC fails to link due to msys2 crt update Ā· Issues Ā· Glasgow Haskell Compiler / GHC Ā· GitLab, fixed in later versions of GHC. EDIT: as a Windows user, you are best advised to use GHC >= 9.4 because of that bug. (I had forgotten it, because it did not affect the Stack-supplied version of MSYS2.)

2 Likes

To follow up on a few other points:

  • GHCup is, in my view, an excellent tool, and an essential one if you are using Cabal (the tool) or the Haskell extension for the popular Visual Studio Code editor - because Cabal does not aim to be an ā€˜installerā€™ (Stack is more of a ā€˜penknifeā€™; it has more than one blade). I would recommend that you donā€™t let this GHC bug put you off it.
  • The GHCup project weighs up various factors before deciding to bump its GHC recommendation. This discussion gives an insight into that: [RFC] Bumping recommended GHC to 9.4.5 in GHCup.
  • Stack is being actively looked after and, I hope, will be developed further - but that activity does depend on volunteers. See Code frequency Ā· commercialhaskell/stack Ā· GitHub
  • If you much prefer MSYS2/bash, and your machine has enough disk space for two operating systems, you might want to explore also Haskell on Linux on Windows Subsystem for Linux 2 (WSL2).
3 Likes

I do most of my development on my Linux host machine. The reason I installed Haskell in the Windows VM was to check if my package builds succesfully on Windows. (I have a bit of platform-specific code that uses the unix and Win32 packages, and up until now Iā€™ve been writing the Win32 part blind.)

1 Like

We have just updated the recommended GHC version to 9.4.7: Bump recommended GHC to 9.4.7 by hasufell Ā· Pull Request #138 Ā· haskell/ghcup-metadata Ā· GitHub

2 Likes