[Haskell-cafe] Unable to call function from DLL using FFI

rajendra prasad rajendradprasad at gmail.com
Wed Mar 14 15:01:43 CET 2012


I am new to Haskell and I need to call a c function by loading dynamic link
library in Haskell. I started with very simple code to create a dll in
visual studio 2008 and then trying to load it in haskell. I am listing here
the steps I follwed to achieve this:

*Step 1: *
My c++ code(HelloWorld.cpp) looks like this:

__declspec(dllexport) int doubleMe(int a)
    return 2*a;

I created the dll using following command:
cl /D_USRDLL /D_WINDLL HelloWorld.cpp  /link  /dll /out:LoadDLL.dll

*Step 2:*
My Haskell code(Main.hs) looks like this:

{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Foreign
import Foreign.C

foreign import ccall "LoadDLL doubleMe" twice :: CInt -> IO (CInt)

main = do
  let x = 4
  y <- twice x
  print y

I am compling haskell code using following command:
ghci Main.hs -L. -lLoadDLL

I am finally getting the following error:

During interactive linking, GHCi couldn't find the following symbol:
This may be due to you not asking GHCi to load extra object files,
archives or DLLs needed by your current session.  Restart GHCi, specifying
the missing library using the -L/path/to/object/dir and -lmissinglibname
flags, or simply by naming the relevant files on the GHCi command line.
Alternatively, this link failure might indicate a bug in GHCi.
If you suspect the latter, please send a bug report to:
  glasgow-haskell-bugs at haskell.org

Please help me in identifying the mistake I am doing. If possible please
let me know the exact steps to be followed to solve this issue.

Thank you so much in advance,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120314/707ed690/attachment.htm>

More information about the Haskell-Cafe mailing list