Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Haskell lib as DLL

I am working with GHC version 7.6.1, following the steps in the docs for creating a DLL from a Haskell lib for using it in VBA: GHC Docs 7.6.1 # Make a DLL

The files I use are the exact same ones as in the Docs. When it comes to compiling, the following to commands work nearly as expected:

ghc -c Adder.hs
ghc -c StartEnd.c

The first command returns this:

Adder.hs:7:1: Warning:
    the 'stdcall' calling convention is unsupported on this platform,
    treating as ccall
    When checking declaration:
      foreign export stdcall "adder" adder :: Int -> Int -> IO Int

Which I guess should be fine...

However, the last command ghc -shared -o Adder.dll Adder.o Adder_stub.o StartEnd.o produces this error:

Adder_stub.h:1:19:  fatal error: HsFFI.h: No such file or directory
compilation terminated.

This thread pointed me towards recognizing that GHC isn't using the header files located in its GHC-HOME/lib/include. The solution in this thread was to use GHC with the option -I [path to include-folder], however, when trying it (and looking at the man and docs) there is no such option for this version of GHC; nor is there in the newer versions up to 8.4.3.

When reading this docs I also encountered that it should be possible to use the option mk-dll under windows to generate dlls; however, according to the man page of my installed GHC, there is no such option, even though I downloaded and installed the windows 64-bit installer.

I then tried to just copy the include folder to some other location; copying the files needed by the third compilation command into the folder too and tried it again. It successfully compiled the DLL;

However, when I try to use the DLL from an Excel(macro-enabled) workbook, I get the error DLL not found. When attempting to reference the DLL in excel (via Tools->References->Browse-> chooseDll) it tells me that it can't add the DLL.

Examining the DLL with DependencyWalker, I get this error:

Error: At least one required implicit or forwarded dependency was not found.
Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.
Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.

Do you have any ideas on how to resolve this? Thanks a lot...

Update

When I try to use the compiled library from C++ as discribed in the docs I end up with this error message:

PS C:\Users\dev\programming\excel_haskell_binding> ghc -o tester.exe .\Tester.cpp .\include\Adder.dll.a
C:\Users\dev\AppData\Local\Temp\ghc4088_0\ghc4088_0.o:ghc4088_0.c:(.text+0x0): multiple definition of `main'
Tester.o:Tester.cpp:(.text+0x0): first defined here
C:\Users\dev\AppData\Local\Temp\ghc4088_0\ghc4088_0.o:ghc4088_0.c:(.text+0x4f): undefined reference to `ZCMain_main_closure'
c:/ghc/ghc-7.6.1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.6.3/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\dev\AppData\Local\Temp\ghc4088_0\ghc4088_0.o: bad reloc address 0x0 in section `.pdata'
collect2: ld returned 1 exit status

Update 2

I am also trying a different approach; see this question GHC compile DLL for use in VBA

like image 970
Fabian Schneider Avatar asked Oct 05 '18 09:10

Fabian Schneider


1 Answers

I was struggling with the same problem, but after a long fight, I managed to run the command from 64bit Excel. Follow these steps:

1) Create Adder.hs (notice ccall, not stdcall):

{-# LANGUAGE ForeignFunctionInterface #-}
module Adder where

adder :: Int -> Int -> IO Int  -- gratuitous use of IO
adder x y = return (x+y)

foreign export ccall adder :: Int -> Int -> IO Int

2) Create StartEnd.c:

#include <Rts.h>

void HsStart()
{
   int argc = 1;
   char* argv[] = {"ghcDll", NULL}; // argv must end with NULL

   // Initialize Haskell runtime
   char** args = argv;
   hs_init(&argc, &args);
}

void HsEnd()
{
   hs_exit();
}

3) Compile these files:

ghc -c Adder.hs
ghc -c StartEnd.c

4) Copy following files from "C:\Program Files\Haskell Platform\8.6.3\lib\include" to your build folder (remember to put stg/Types.h into stg folder). You may also copy all the contents of /include folder if you wish:

HsFFI.h
ghcconfig.h
ghcautoconf.h
ghcplatform.h
stg/Types.h

5) Create the dll:

ghc -shared -o Adder.dll Adder.o Adder_stub.h StartEnd.o

6) Now open the Excel and use this macro (use first HsStart before Adder). Remember to link to your own folder:

Private Declare PtrSafe Function Adder Lib "C:\Users\User\haskell\Adder.dll" Alias "adder" _
  (ByVal x As Long, ByVal y As Long) As Long

Private Declare PtrSafe Sub HsStart Lib "C:\Users\User\haskell\Adder.dll" ()
Private Declare PtrSafe Sub HsEnd Lib "C:\Users\User\haskell\Adder.dll" ()

Private Sub Document_Close()
HsEnd
End Sub

Private Sub Document_Open()
HsStart
End Sub

Public Sub Test()
MsgBox "12 + 5 = " & Adder(12, 5)
End Sub

7) Be amazed!

like image 142
A. Soikkeli Avatar answered Oct 02 '22 08:10

A. Soikkeli