vendredi 29 mai 2015

OCaml - Compile OCaml and C code that uses Ctypes

I'm trying to learn how to call routines in C directly from OCaml code, using the Ctypes library.

I have this basic example with two files: hello.ml and hello.c.

hello.ml looks like this:

open Ctypes
open Foreign

let hello = 
    foreign "hello" (float @ -> returning void)
;;

let () = 
    hello 3.15
;;

hello.c looks like this:

#include <stdio.h>

void hello(double x)
{
    if ( x > 0)
        printf("hello!\n");
}

How do I compile these two files into one executable?

The process of manually compiling/linking code is scary to me and I don't understand it very well. I usually use a Makefile template to compile my code because that's really easy.

Aucun commentaire:

Enregistrer un commentaire