Skip to content

Instantly share code, notes, and snippets.

@Happy-Ferret Happy-Ferret/lib.hs
Last active Nov 8, 2016

Embed
What would you like to do?
Mixed assembly (C/Haskell)
{-# LANGUAGE ForeignFunctionInterface #-}
module Test where
import Foreign.C.Types
hsfun :: CInt -> IO CInt
hsfun x = do
putStrLn "Hello from haskell"
return (42 * x)
hsfoo :: CInt -> IO CInt
hsfoo x = do
putStrLn "Hello foo"
return (1024 * x)
foreign export ccall hsfun :: CInt -> IO CInt
foreign export ccall hsfoo :: CInt -> IO CInt
# Creates a shared library called libMixed.so
ghc -O2 -dynamic -shared -fPIC -o libMixed.so lib.hs module_init.c -lHSrts-ghc7.10.3
#include <HsFFI.h>
extern void __stginit_Test(void);
static void library_init(void) __attribute__((constructor));
static void library_init(void)
{
static char *argv[] = { "libMixed.so", 0 }, **argv_ = argv;
static int argc = 1;
hs_init(&argc, &argv_);
hs_add_root(__stginit_Test);
}
static void library_exit(void) __attribute__((destructor));
static void library_exit(void)
{
hs_exit();
}
int add(int a, int b) {
return a + b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.