Skip to content

Instantly share code, notes, and snippets.

@merijn
Created August 23, 2014 07:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merijn/4a0fee2b3a5ef3476aa4 to your computer and use it in GitHub Desktop.
Save merijn/4a0fee2b3a5ef3476aa4 to your computer and use it in GitHub Desktop.
Minimal C main FFI example
Name: Foo
Version: 0.1.0
Homepage:
Bug-Reports:
Author: John Smith
Maintainer: John Smith <john@example.com>
Copyright: Copyright © 2013 John Smith
License: LGPL-3
License-File: LICENSE.LESSER
Category: System
Cabal-Version: >= 1.17
Build-Type: Simple
Tested-With: GHC == 7.6.3
Synopsis:
Description:
Extra-Source-Files: LICENSE
Executable foo
Default-Language: Haskell2010
GHC-Options: -Wall -threaded -no-hs-main
GHC-Prof-Options: -auto-all -caf-all -rtsopts
Main-Is: main.c
Other-Modules: Foo
C-Sources:
CC-Options: -Wall -Wextra -pedantic -strict -std=c99
Build-Depends: base
{-# LANGUAGE ForeignFunctionInterface #-}
module Foo where
import Foreign.C.String
import Foreign.C.Types
foreign export ccall start_server :: CInt -> CString -> IO ()
foreign export ccall start_client :: CString -> IO ()
start_server :: CInt -> CString -> IO ()
start_server pid = undefined
start_client :: CString -> IO ()
start_client = undefined
#include <HsFFI.h>
#ifdef __GLASGOW_HASKELL__
#include "Foo_stub.h"
extern void __stginit_Foo(void);
#endif
int
main(int argc, char **argv)
{
hs_init(&argc, &argv);
#ifdef __GLASGOW_HASKELL__
hs_add_root(__stginit_Foo);
#endif
/* call functions defined in module Foo.hs here */
hs_exit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment