我正在弄乱OCaml FFI试图弄清楚如何推断C enum的宽度(我认为是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 ();;
Run Code Online (Sandbox Code Playgroud)
我配置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
Run Code Online (Sandbox Code Playgroud)
我用来编写一个简单.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
Run Code Online (Sandbox Code Playgroud)
为什么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;;
Run Code Online (Sandbox Code Playgroud)