OCaml - Compile OCaml and C code that uses Ctypes -
i'm trying learn how call routines in c directly ocaml code, using ctypes
library.
i have basic example 2 files: hello.ml
, hello.c
.
hello.ml
looks this:
open ctypes open foreign let hello = foreign "hello" (float @ -> returning void) ;; let () = hello 3.15 ;;
hello.c
looks this:
#include <stdio.h> void hello(double x) { if ( x > 0) printf("hello!\n"); }
how compile these 2 files 1 executable?
the process of manually compiling/linking code scary me , don't understand well. use makefile template compile code because that's easy.
here's example use on os x.
in simple.c
int adder(int a, int b) { return + b; }
and in simple.ml
open ctypes open foreign let adder_ = foreign "adder" (int @-> int @-> returning int) let () = print_endline (string_of_int (adder_ 1 2))
then
clang -shared simple.c -o simple.so ocamlfind ocamlopt -package ctypes.foreign -cclib simple.so -linkpkg simple.ml -o test ./test
and should print out 3 on terminal.
Comments
Post a Comment