Haskell ghc visual studio DLL weird behaviour

I am trying to call a C DLL built with visual studio (2015 community edition) from GHC (version 7.8.3) on windows.

C code: (myDLL.cpp)

extern "C" {
    __declspec(dllexport) int someFn() {
        return 42;
    }
}

Haskell code: (Main.hs)

module Main where

import Foreign.Ptr
import Foreign.C.Types 
import Data.Int

foreign import ccall unsafe "someFn" c_someFn :: CInt
mySomeFn = c_someFn

main :: IO ()
main = do let z = mySomeFn
          print z

after copying the generated x64 .dll and .lib and .hs file to the same folder, and running

 ghci -L. -lmyDLL Main.hs
 ...
 >main

It prints '42' as expected.

Running

ghc -L. -lmyDLL Main.hs

compiles and links fine, but after executing main.exe, no output is printed. Not even an error message. Why does this happen?

EDIT: Here's what I did to fix it. Install latest Haskell Platform, then download and copy the latest copy of binutils to the mingw folder of haskell platform. (D:Program FilesHaskell Platform7.10.2-amingw for me). Next, it started complaining about zlib1.dll, so I also downloaded that from MSYS2 and pasted it in the mingwbin folder. After that, it worked.


Seems similar to this issue. Basically, ghc-7.8 distribution contains buggy binutils, they don't handle Visual Studio .lib files properly. The workaround is to upgrade mingw distribution or regenerate the .lib file. See the ticket (and also #10726) for details.

链接地址: http://www.djcxy.com/p/33334.html

上一篇: 读GHC核心

下一篇: Haskell ghc visual studio DLL奇怪的行为