首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导出Haskell库作为DLL

导出Haskell库作为DLL
EN

Stack Overflow用户
提问于 2018-10-05 09:25:54
回答 1查看 489关注 0票数 6

我正在使用GHCVersion7.6.1,按照文档中的步骤从Haskell创建一个DLL,以便在VBA:GHC文档7.6.1 #创建一个DLL中使用它

我使用的文件与Docs中的文件完全相同。当涉及编译时,以下命令的工作方式与预期几乎相同:

代码语言:javascript
复制
ghc -c Adder.hs
ghc -c StartEnd.c

第一个命令返回如下:

代码语言:javascript
复制
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会产生以下错误:

代码语言:javascript
复制
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,我得到以下错误:

代码语言:javascript
复制
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++的编译库作为文档中的描述时,我最终得到了以下错误消息:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-14 21:57:29

我正努力解决同样的问题,但经过长时间的斗争,我成功地从64位Excel运行了这个命令。遵循以下步骤:

1)创建Adder.hs (注意ccall,而不是stdcall):

代码语言:javascript
复制
{-# 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)创建StartEnd.c:

代码语言:javascript
复制
#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)汇编这些档案:

代码语言:javascript
复制
ghc -c Adder.hs
ghc -c StartEnd.c

4)将以下文件从"C:\Program \Haskell Platform\8.6.3\lib\include“复制到构建文件夹(请记住将stg/Types.h放入stg文件夹)。如果您愿意,还可以复制/include文件夹的所有内容:

代码语言:javascript
复制
HsFFI.h
ghcconfig.h
ghcautoconf.h
ghcplatform.h
stg/Types.h

5)创建dll:

代码语言:javascript
复制
ghc -shared -o Adder.dll Adder.o Adder_stub.h StartEnd.o

6)现在打开Excel并使用这个宏(先使用HsStart,再使用Adder)。请记住链接到您自己的文件夹:

代码语言:javascript
复制
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

惊讶吧!

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52662440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档