我正在使用GHCVersion7.6.1,按照文档中的步骤从Haskell创建一个DLL,以便在VBA:GHC文档7.6.1 #创建一个DLL中使用它
我使用的文件与Docs中的文件完全相同。当涉及编译时,以下命令的工作方式与预期几乎相同:
ghc -c Adder.hs
ghc -c StartEnd.c第一个命令返回如下:
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我想应该没问题..。
但是,最后一个命令ghc -shared -o Adder.dll Adder.o Adder_stub.o StartEnd.o会产生以下错误:
Adder_stub.h:1:19: fatal error: HsFFI.h: No such file or directory
compilation terminated.这个帖子帮助我认识到,GHC没有使用位于其GHC-HOME/lib/include中的头文件。这个线程中的解决方案是在选项-I [path to include-folder]中使用GHC,但是,当尝试它(并查看man和docs)时,这个版本的GHC没有这样的选项;在8.4.3以前的新版本中也没有这样的选项。
当读这份文档还遇到使用windows下的选项mk-dll生成dll时,但是,根据我已安装的GHC的手册页,即使我下载并安装了windows 64位安装程序,也没有这样的选项。
然后,我尝试将include文件夹复制到其他位置;将第三个编译命令所需的文件也复制到文件夹中,然后再试一次。成功编译了动态链接库;
但是,当我尝试使用Excel(启用宏的)工作簿中的DLL时,无法找到错误DLL。当试图引用excel中的DLL时(通过Tools-> reference ->Browse-> chooseDll),它告诉我它不能添加DLL。
使用DependencyWalker检查DLL,我得到以下错误:
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.你对如何解决这个问题有什么想法吗?非常感谢..。
更新
当我尝试使用来自C++的编译库作为文档中的描述时,我最终得到了以下错误消息:
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更新2
我也在尝试一种不同的方法;参见这个问题用于VBA的GHC编译DLL
发布于 2018-12-14 21:57:29
我正努力解决同样的问题,但经过长时间的斗争,我成功地从64位Excel运行了这个命令。遵循以下步骤:
1)创建Adder.hs (注意ccall,而不是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 Int2)创建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)汇编这些档案:
ghc -c Adder.hs
ghc -c StartEnd.c4)将以下文件从"C:\Program \Haskell Platform\8.6.3\lib\include“复制到构建文件夹(请记住将stg/Types.h放入stg文件夹)。如果您愿意,还可以复制/include文件夹的所有内容:
HsFFI.h
ghcconfig.h
ghcautoconf.h
ghcplatform.h
stg/Types.h5)创建dll:
ghc -shared -o Adder.dll Adder.o Adder_stub.h StartEnd.o6)现在打开Excel并使用这个宏(先使用HsStart,再使用Adder)。请记住链接到您自己的文件夹:
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惊讶吧!
https://stackoverflow.com/questions/52662440
复制相似问题