将Haskell lib导出为DLL [英] Export Haskell lib as DLL

查看:92
本文介绍了将Haskell lib导出为DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GHC版本7.6.1,请按照文档中的步骤操作,以从Haskell库创建DLL以在VBA中使用它:
GHC文件7.6.1#制作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...

但是,最后一个命令 ghc -shared -o Adder.dll Adder.o Adder_stub.o StartEnd.o 会产生此错误:

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.

该线程使我认识到GHC并未使用其 GHC-HOME / lib / include 中的头文件。 code>。
此线程中的解决方案是将GHC与选项 -I [包含文件夹的路径] 一起使用,但是,在尝试时(并看着那个男人和文档)此版本的GHC没有此类选项;

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.

阅读此文档我还遇到了应该可以使用 mk-dll 在Windows下生成dll;但是,根据我安装的GHC的手册页,即使下载并安装了Windows 64位安装程序,也没有这种选择。

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.

然后,我尝试只需将include文件夹复制到其他位置;将第三个编译命令所需的文件也复制到该文件夹​​中,然后再次尝试。它成功地编译了DLL;

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;

但是,当我尝试使用Excel(启用宏的工作簿)中的DLL时,出现错误DLL找不到。尝试在excel中引用DLL时(通过Tools-> References-> Browse-> choiceDll),它告诉我无法添加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.

带有DependencyWalker的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...

当我尝试从正如文档中所述的C ++,我最终收到此错误消息:

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



更新2



我也在尝试另一种方法;看到此问题在VBA中使用的GHC编译DLL

推荐答案

我在同样的问题上苦苦挣扎,但是经过长时间的战斗,我设法从64位Excel中运行了该命令。请按照下列步骤操作:

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)创建Adder.hs(注意ccall,而不是stdcall):

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)创建StartEnd.c:

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)编译以下文件:

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

4)将以下文件从 C:\Program Files\Haskell Platform\8.6.3\lib\include复制到您的生成文件夹中(记住要放置stg / Types。 h进入stg文件夹)。如果愿意,也可以复制/ include文件夹的所有内容:

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)创建dll:

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

6)现在打开Excel并使用此宏(在Adder之前使用第一个HsStart)。记住要链接到您自己的文件夹:

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)感到惊讶!

这篇关于将Haskell lib导出为DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆