Edg*_*ian 5 c ocaml shared-libraries gnu-make
我正在探索一些冒险的想法.
TL:DR; gnumake能够使用可加载的模块,我试图使用该C屏障来使用OCaml但是在OCaml运行时初始化时遇到了麻烦.
我有这个OCaml代码:
(* 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
Run Code Online (Sandbox Code Playgroud)
我也有这个C代码:(是的,需要使用额外的CAML
宏,但这里不相关)
#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);
/* receive_arg */
return buf;
}
int do_speak_gmk_setup()
{
printf("Getting Called by Make\n");
// This is pretty critical, will explain below
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;
}
Run Code Online (Sandbox Code Playgroud)
我正在用这个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}
Run Code Online (Sandbox Code Playgroud)
而我的问题是,只有printf("Getting Called by Make\n");
当我load do_speak.so
在Makefile中添加相应的内容时,caml_startup
才会正常运行.现在我打电话,caml_startup
因为如果我没有,那么我得到一个错误
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.
Run Code Online (Sandbox Code Playgroud)
这是因为OS X上的clang链接的方式,请参阅此处了解更多详细信息:http://psellos.com/2014/10/2014.10.atom-table-undef.html
我有点想法......我需要用OCaml代码创建一个C共享库,然后需要成为另一个C共享库的一部分,我显然没有caml_startup想要的原始argv指针.正如我的代码示例显示,我已经试过假装出来,并且还使用caml_startup(NULL)
与char **argv = {NULL}; caml_startup(argv)
具有类似缺乏成功的.我不知道如何正确初始化运行时.
归档时间: |
|
查看次数: |
192 次 |
最近记录: |