Created
May 17, 2021 18:41
-
-
Save derrickturk/30ff02edf63759da4f986464c827a624 to your computer and use it in GitHub Desktop.
minimal example of building a DLL with GHC Haskell and calling it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EXPORTS | |
fmaInt | |
hs_init | |
hs_exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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