Hello all!
I have some function written in OCaml that I need to call in a C program.
The OCaml function takes a string, performs some computation and also
outputs a string. Note that these types are not mandatory, just for testing purposes.
Therefore, I defined a C function like:
char * foo (char * inp) {
CAMLlocal1 (caml_inp) ;
caml_inp = caml_copy_string (inp);
value * func = caml_named_value (“foo") ;
if (func == NULL)
puts (“Unable to load OCaml function!") ;
return strdup(String_val(caml_callback(*func, oinp)));
}
I am able to correctly compile the file with:
ocamlc -c foo.c (according to the manual)
Next, I define the main.c file as:
extern char * foo(char * inp);
int main (int argc, char ** argv)
{
/* Initialize OCaml code */
caml_startup(argv);
/* Do some computation */
char * inp = “some string";
printf ("%s\n", foo(inp));
return 0;
}
However, I’m not able to compile the main.c file!
The error output is the following:
Undefined symbols for architecture x86_64:
"_caml_startup", referenced from:
_main in main-05d208.o
“_foo", referenced from:
_main in main-05d208.o
I followed the instructions in the manual to interop C and OCaml when
the main program is in the C side.
Nevertheless, If I run the example in the manual, it works very well!
Any idea on what am I doing wrong?
Best,
Vitor