我在弄乱OCaml FFI,试图弄清楚如何推断C枚举的宽度(我认为这是C的实现定义的),并试图插入错误宽度的类型以查看会造成什么打击在运行时。这是动机,但是我遇到的实际问题要平凡得多。
我有一个简单的OCaml文件,该文件使用C FFI调用一个琐碎的函数,example.c
该函数将枚举转换为int。
open Printf;;
let (@->) = Ctypes.(@->);;
let returning = Ctypes.returning;;
let foreign = Foreign.foreign;;
(* deliberately use the wrong scalar type for argument *)
let wrong_int64_of_color =
foreign "int_of_color" (Ctypes.int64_t @-> returning Ctypes.int64_t);;
let main () =
printf "%Ld\n" (wrong_int64_of_color (Int64.of_int 100));;
let () = main ();;
我配置OPAM并安装Ctypes
和Ctypes.Foreign
% opam config env | sed -e 's/=.*/=/'
CAML_LD_LIBRARY_PATH=
OPAMUTF8MSGS=
MANPATH=
PERL5LIB=
OCAML_TOPLEVEL_PATH=
PATH=
% opam list | grep ctypes
ctypes 0.6.2 Combinators for binding to C libraries withou
ctypes-foreign 0.4.0 Virtual package for enabling the ctypes.forei
我用来编译一个简单.ml
脚本的两个通常的咒语都使我失望,而且我没有主意。ocamlfind
和corebuild
(我认为这是顶部的包装器ocamlbuild
)
ocamlfind似乎找不到ctypes
和foreign
。然而,这并不抱怨有关无法找到包,所以我猜测ctypes
,并ctypes.foreign
是在怪异这些包正确的名称findlib
命名。
% ocamlfind ocamlopt -package findlib,ctypes,ctypes.foreign -thread call_example.ml
File "_none_", line 1:
Warning 58: no cmx file was found in path for module Foreign, and its interface was not compiled with -opaque
File "call_example.ml", line 1:
Error: No implementations provided for the following modules:
Ctypes referenced from call_example.cmx
Foreign referenced from call_example.cmx
为什么ocamlfind
找不到这些模块?我将它们加载到顶层没有问题。
─( 22:30:42 )─< command 0
utop # #require "ctypes";;
─( 22:30:42 )─< command 1
utop # open Ctypes;;
─( 22:30:55 )─< command 2
utop # #require "ctypes.foreign";;
─( 22:31:00 )─< command 3
utop # open Ctypes;;
在我看来,您忘记了添加-linkpkg
ocamlfind选项来指示编译器实际上将软件包中的库链接起来以生成可执行文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句