Created
August 23, 2014 07:01
-
-
Save merijn/4a0fee2b3a5ef3476aa4 to your computer and use it in GitHub Desktop.
Minimal C main FFI example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Name: Foo | |
Version: 0.1.0 | |
Homepage: | |
Bug-Reports: | |
Author: John Smith | |
Maintainer: John Smith <[email protected]> | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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