I am exploring some adventurous ideas.
TL: DR; gnumake can use loadable modules, I'm trying to use this C barrier to use OCaml, but you have problems initializing the OCaml environment.
I have this OCaml code:
(* This is speak_ocaml.ml *)
let do_speak () =
print_endline "This called from OCaml!!";
flush stdout;
"Some return value from OCaml"
let () =
Callback.register "speak" do_speak
and I also have this C code: (Yes, I need to use additional macros CAML, but not relevant here)
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <gnumake.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <caml/memory.h>
#include <caml/alloc.h>
int plugin_is_GPL_compatible;
char *ocaml_speaker(const char *func_name, int argc, char **argv)
{
char *answer =
String_val(caml_callback(*caml_named_value("speak"), Val_unit));
printf("Speaking and got: %s\n", answer);
char *buf = gmk_alloc(strlen(answer) + 1);
strcpy(buf, answer);
return buf;
}
int do_speak_gmk_setup()
{
printf("Getting Called by Make\n");
char **argv = {"/home/Edgar/foo", NULL};
caml_startup(argv);
printf("Called caml_startup\n");
gmk_add_function("speak", ocaml_speaker, 1, (unsigned int)1, 1);
return 1;
}
and I compile it using this makefile
all:
ocamlopt -c speak_ocaml.ml
ocamlopt -output-obj -o caml_code.o speak_ocaml.cmx
clang -I`ocamlc -where` -c do_speak.c -o do_speak.o
clang -shared -undefined dynamic_lookup -fPIC -L`ocamlc -where` -ldl \
-lasmrun do_speak.o caml_code.o -o do_speak.so
show_off:
echo "Speaker?"
${speak 123}
clean:
@rm -rf *.{cmi,cmt,cmi,cmx,o,cmo,so}
And my problem is that it only printf("Getting Called by Make\n");turns off when I add the appropriate one load do_speak.soto the Makefile, it caml_startupdoesn't work correctly. Now I call caml_startup, because if I do not, I get an error
Makefile:9: dlopen(do_speak.so, 9): Symbol not found: _caml_atom_table
Referenced from: do_speak.so
Expected in: flat namespace
in do_speak.so
Makefile:9: *** do_speak.so: failed to load. Stop.
- , clang OS X , . : http://psellos.com/2014/10/2014.10.atom-table-undef.html
... C- OCaml, C, , , argv, caml_startup . , , caml_startup(NULL) char **argv = {NULL}; caml_startup(argv) . , .