Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Created May 17, 2021 18:41
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 derrickturk/30ff02edf63759da4f986464c827a624 to your computer and use it in GitHub Desktop.
Save derrickturk/30ff02edf63759da4f986464c827a624 to your computer and use it in GitHub Desktop.
minimal example of building a DLL with GHC Haskell and calling it
EXPORTS
fmaInt
hs_init
hs_exit
{-# LANGUAGE ForeignFunctionInterface #-}
module HsDll where
import Foreign.C.Types
foreign export ccall fmaInt :: CInt -> CInt -> CInt -> CInt
fmaInt :: CInt -> CInt -> CInt -> CInt
fmaInt x y z = x * y + z
# there is probably a way to get this from "stack path"
INCLUDE=C:\Users\Derrick\AppData\Local\Programs\stack\x86_64-windows\ghc-8.8.4\lib\include
all: hsdll.dll hsdll_stub.h usehsdll.exe
%.dll %_stub.h: %.hs %.def
stack ghc -- $^ --make -O2 -shared -static -o $@
%.exe: %.c hsdll_stub.h hsdll.dll
gcc -O2 -I$(INCLUDE) -Wall -Wextra -std=c11 -pedantic $^ hsdll.dll -o $@
clean:
-rm *.dll *.exe
#include <stdio.h>
#include "hsdll_stub.h"
int main(void)
{
hs_init(0, 0);
printf("fmaInt(3, 4, 5) = %d\n", fmaInt(3, 4, 5));
hs_exit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment