Skip to content

Instantly share code, notes, and snippets.

@quyse
Last active February 4, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quyse/ffc24642ff0339b4c503 to your computer and use it in GitHub Desktop.
Save quyse/ffc24642ff0339b4c503 to your computer and use it in GitHub Desktop.
Haskell Knowledge

Bypassing TH build errors on Windows when linking with C++

GHCi linker (which is used by TH) cannot load some C++ code dynamically for some reason. It's unclear whether these GHC tickets are really fixed: GHC 3242, GHC 10726.

Workaround:

  • Put list of unresolved externals into all.def (add others if needed):
EXPORTS
	__mingw_vsprintf
	__mingw_vsnprintf
	strdup
	__popcountdi2
	snprintf
	mingw_getsp
	_Unwind_Resume

  • Perform: stack exec gcc -- all.def -shared -oall.dll
  • Put all.dll into GHC lib path (mingw/lib).
  • Add extra-ghci-libraries: all to package which uses C++.

GHC 7.10.1 built with integer-simple

EXPORTS
    __mingw_vsprintf
    __mingw_vsnprintf
    strdup
    __popcountdi2
    snprintf
    mingw_getsp
    __gxx_personality_sj0
    _Unwind_SjLj_Register
    _Unwind_SjLj_Unregister
    _Unwind_SjLj_Resume

stack exec gcc -- all.def -lstdc++ -shared -oall.dll

Plus in GHC 7.10.1 there's a bug leading to compiler crash with message like: checkProddableBlock: invalid fixup in runtime linker. The workaround I know is to patch GHC:

diff --git a/rts/Linker.c b/rts/Linker.c
index 0bd2aa8..18762f6 100644
--- a/rts/Linker.c
+++ b/rts/Linker.c
@@ -4684,7 +4684,7 @@ ocResolve_PEi386 ( ObjectCode* oc )
            foundit:;
          }
          /* All supported relocations write at least 4 bytes */
-         checkProddableBlock(oc, pP, 4);
+         // checkProddableBlock(oc, pP, 4); // commented by quyse
          switch (reltab_j->Type) {
 #if defined(i386_HOST_ARCH)
             case MYIMAGE_REL_I386_DIR32:

GHC 7.10.3

Looks like all you need is

EXPORTS
    __popcountdi2
    __mingw_strtod
    _ZdlPv
    __cxa_pure_virtual

__mingw_strtod is apparently needed for some C code, specifically lua reference implementation. _ZdlPv and __cxa_pure_virtual are needed for C++ code (libsquish in flaw-asset-dxt).

SDL on Windows

stack exec curl -- -O http://libsdl.org/release/SDL2-2.0.3.zip
stack exec bash -- ./configure --build=x86_64-w64-mingw32 --disable-directx --disable-render-d3d --prefix=/
stack exec make

Well, doesn't work, stack doesn't want to pick it up.

GHC 8.0.2

New workaround for C++ linking errors:

  if os(windows)
    if arch(x86_64)
      extra-ghci-libraries: gcc_s_seh-1
    else
      extra-ghci-libraries: gcc_s_dw2-1

Also compilation crashes. Workaround is to use -fexternal-interpreter.

$STACK_ROOT/config.yaml

ghc-options:
  "*": -O3 -threaded -with-rtsopts=-N -Wall -fno-warn-unsupported-calling-conventions -fno-warn-tabs -fexternal-interpreter

rebuild-ghc-options: true
apply-ghc-options: everything

dump-logs: all

build:
  split-objs: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment