这是此问题的替代方法:Export Haskell lib as DLL
我正在使用GHC版本8.6.1 [最新],并遵循the documentation来从haskell库中编译DLL,以便随后在VBA中使用。
我的文件如下:
Adder.hs:
{-# LANGUAGE ForeignFunctionInterface #-}
module Adder where
adder :: Int -> Int -> IO Int
adder x y = return (x+y)
foreign export ccall adder :: Int -> Int -> IO Int
StartEnd.c:
#include <Rts.h>
void HsStart() {
int argc = 1;
char* argv[] = {"ghcDll", NULL};
char** args = argv;
hs_init(&argc, &args);
}
void HsEnd() {
hs_exit();
}
我将文件夹ghc-8.6.1\lib\include
复制到了构建位置,并将两个文件复制到了该文件夹中(因为我无法弄清楚如何正确传递-I [PATH]参数)。
运行这些编译步骤,我得到:
ghc -c Adder.hs
--> no error
ghc -c StartEnd.c
--> no error
ghc -shared -no-hs-main -o Adder.dll Adder.o Adder_stub.h StartEnd.o
--> no error
和文件
Adder.dll
Adder.dll.a
Adder.hi
Adder.o
Adder_stub.h
startEnd.o
我尝试使用文档ghc -shared -o Adder.dll Adder.o Adder_stub.h StartEnd.o
中的第三个编译命令,并使用参数-no-hs-main来确定。
我的VBA脚本如下:
Private Declare Function Adder Lib "PATH\TO\Adder.dll" Alias "adder" _
(ByVal x As Long, ByVal y As Long) As Long
Private Declare Sub HsStart Lib "PATH\TO\Adder.dll" ()
Private Declare Sub HsEnd Lib "PATH\TO\Adder.dll" ()
Public Sub test()
HsStart
MsgBox "12 + 5 = " & Adder(12, 5)
HsEnd
End Sub
但是,每当我尝试运行Sub测试时,都会收到错误消息:
Run-Time error '48'
File not found:
PATH\TO\Adder.dll
您能告诉我我在哪里犯了错误以及如何解决吗?
如果有帮助,dumpbin /EXPORTS Adder.dll
的输出是这样的:
Microsoft (R) COFF/PE Dumper Version 14.00.24234.1
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file Adder.dll
File Type: DLL
Section contains the following exports for Adder.dll
00000000 characteristics
5BBD9533 time date stamp Wed Oct 10 07:59:15 2018
0.00 version
1 ordinal base
29737 number of functions
29737 number of names
ordinal hint RVA name
1 0 004E0F18 ALLOC_BH_adm
...
75 4A 00001681 HsEnd
76 4B 00001640 HsStart
...
472 1D7 00001540 adder
...
我的路径:
C:\Users\scfa\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin;C:\Users\scfa\Documents\myProgramms\dev_msys2\mingw64\bin;C:\Users\scfa\Documents\myProgramms\dev_msys2\mingw32\bin;C:\Users\scfa\Documents\myProgramms\ghc-8.6.1\bin;C:\Users\scfa\Documents\myProgramms\ghc-8.6.1\mingw\bin