Haskell c function call compared to Python

In Python, I can do this:

import ctypes
import ctypes.util

my_lib = ctypes.cdll.LoadLibrary (ctypes.util.find_library ('my_lib') or 'my_lib32')
a = my_lib.some_function(33)
b = my_lib.some_function2(33)
c = my_lib.SOME_CONST_123

And since I need to convert this type of Python code to Haskell, I wonder if I can do the same in Haskell? I know I can do something like this FFI. But this is not exactly what I can do in Python, because as far as I know in Haskell, I must first declare the functions as follows:

foreign import ccall "my_lib.h some_function"
     some_function :: CDouble -> CDouble

Is it true, is there an easier way?

+4
source share
1 answer

ctypes Python C libffi, , , , .

Haskell :

import Foreign.LibFFI
import System.Posix.DynamicLinker

example :: IO ()
example = do
  dlopen "mylib.so" [RTLD_LAZY]
  fn  <- dlsym Default "some_function"
  fn2 <- dlsym Default "some_function2"
  a <- callFFI fn retCInt [argCInt 33]
  b <- callFFI fn2 retCInt [argCInt 33]
  return ()

, , , , Python ctypes , . ( , ctypes) . ccall unsafe FFI Haskell, , C.

-, Haskell, Int Float , libffi sum Arg, .

C , Haskell FFI (-XForeignFunctionInterface) / , c2hs, .

+5

All Articles