0
我正在搞砸OCaml FFI以試圖弄清楚它是如何推斷C枚舉的寬度(我認爲是C實現定義的),而且是上午試圖插入一個錯誤寬度的類型來查看在運行時發生了什麼。這是動機,但我遇到的實際問題更爲普遍。ocamlfind抱怨沒有爲通過opam安裝的模塊提供的實現
我有一個簡單的OCaml文件,它使用C FFI調用example.c
中的一個微不足道的函數,它將enum轉換爲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似乎無法找到和foreign
。然而,它並沒有抱怨說無法找到包,所以我猜和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;;