Skip to content

Commit

Permalink
Add Haskell FFI example
Browse files Browse the repository at this point in the history
  • Loading branch information
nh2 committed Oct 28, 2012
1 parent b3b2d2d commit a0b85a2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions experiments/ffi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.PHONY: all
all: c_functions.o

CC=clang

c_functions.o:
$(CC) -c c-functions.c


.PHONY: clean
clean:
rm -f c-functions.o
7 changes: 7 additions & 0 deletions experiments/ffi/c-functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "c-functions.h"


int c_add(int i, int j)
{
return i + j;
}
6 changes: 6 additions & 0 deletions experiments/ffi/c-functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef C_FUNCTIONS_H
#define C_FUNCTIONS_H

int c_add(int i, int j);

#endif
11 changes: 11 additions & 0 deletions experiments/ffi/hs-functions.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE ForeignFunctionInterface #-}

module FfiExperiment where

import Foreign.C


foreign import ccall "c-functions.h c_add" c_add :: CInt -> CInt -> CInt

add :: Int -> Int -> Int
add x y = fromIntegral $ c_add (fromIntegral x) (fromIntegral y)

0 comments on commit a0b85a2

Please sign in to comment.