OCaml生成错误:键入int而不是type unit

OCaml生成错误:键入int而不是type unit,ocaml,ffi,Ocaml,Ffi,我想尝试OCaml FFI并绑定一些C函数,比如 ncurses教程,但我选择了ecore_evas(efl): 以下是我想要使用的函数: void ecore_main_loop_quit(void) void ecore_main_loop_begin(void) int ecore_evas_init(void) int ecore_evas_shutdown(void) Ecore_Evas * ecore_evas_new(const char *, int, int, int, in

我想尝试OCaml FFI并绑定一些C函数,比如 ncurses教程,但我选择了ecore_evas(efl):

以下是我想要使用的函数:

void ecore_main_loop_quit(void)
void ecore_main_loop_begin(void)
int ecore_evas_init(void)
int ecore_evas_shutdown(void)
Ecore_Evas * ecore_evas_new(const char *, int, int, int, int, const char *)
ecore_evas_title_set(const Ecore_Evas *, const char *)
ecore_evas_show(ee)
ecore_evas_free(ee)
因此,我创建文件ecore_evas.ml时使用以下内容:

open Ctypes
open Foreign
let ecore_main_loop_begin = foreign "ecore_main_loop_begin" (void @->
returning void)
let ecore_main_loop_quit = foreign "ecore_main_loop_quit" (void @->
returning void)
let ecore_evas_init = foreign "ecore_evas_init" (void @-> returning int)
let ecore_evas_shutdown = foreign "ecore_evas_shutdown" (void @->
returning int)
type ecore_evas = unit ptr
let ecore_evas : ecore_evas typ = ptr void
let ecore_evas_new = foreign "ecore_evas_new" (string @-> int @-> int
@-> int @-> int @-> string @-> returning ecore_evas)
let ecore_evas_title_set = foreign "ecore_evas_title_set" (ecore_evas
@-> string @-> returning void)
let ecore_evas_show = foreign "ecore_evas_show" (ecore_evas @->
returning void)
let ecore_evas_free = foreign "ecore_evas_free" (ecore_evas @->
returning void)
然后,我使用以下命令创建了一个mli文件:

corebuild -pkg ctypes.foreign ecore_evas.inferred.mli
cp _build/ecore_evas.inferred.mli ./
ecore_evas.mli

val ecore_main_loop_begin : unit -> unit
val ecore_main_loop_quit : unit -> unit
val ecore_evas_init : unit -> int
val ecore_evas_shutdown : unit -> int
type ecore_evas = unit Ctypes.ptr
val ecore_evas : ecore_evas Ctypes_static.typ
val ecore_evas_new : bytes -> int -> int -> int -> int -> bytes ->
ecore_evas
val ecore_evas_title_set : ecore_evas -> bytes -> unit
val ecore_evas_show : ecore_evas -> unit
val ecore_evas_free : ecore_evas -> unit
我创建了一个ecore_evas_window.ml,其中包含:

open Ecore_evas
let () =
ecore_main_loop_begin ();
ecore_evas_init ();
let ee = ecore_evas_new "" 50 50 300 300 "" in
ecore_evas_title_set ee "This is a test";
ecore_evas_show ee
然后我试着用

corebuild -pkg ctypes.foreign -lflags -cclib,-lecore_evas
ecore_evas_window.native
但我有一条错误信息:

+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g
-bin-annot -short-paths -thread -package ctypes.foreign -package core
-ppx 'ppx-jane -as-ppx' -o ecore_evas_window.cmo ecore_evas_window.ml
File "ecore_evas_window.ml", line 4, characters 2-21:
Error: This expression has type int but an expression was expected of type
unit
Command exited with code 2.
Hint: Recursive traversal of subdirectories was not enabled for this build,
as the working directory does not look like an ocamlbuild project (no
'_tags' or 'myocamlbuild.ml' file). If you have modules in
subdirectories,
you should add the option "-r" or create an empty '_tags' file.

To enable recursive traversal for some subdirectories only, you can
use the
following '_tags' file:

true: -traverse
<dir1> or <dir2>: traverse

Compilation unsuccessful after building 4 targets (3 cached) in 00:00:00.
+ocamlfind ocamlc-c-w A-4-33-40-41-42-43-34-44-严格顺序-g
-bin annot-短路径-线程-包ctypes.foreign-包核心
-ppx'ppx jane-as ppx'-o ecore_evas_window.cmo ecore_evas_window.ml
文件“ecore_evas_window.ml”,第4行,字符2-21:
错误:此表达式的类型为int,但表达式的类型应为int
单元
命令已退出,代码为2。
提示:此生成未启用子目录的递归遍历,
因为工作目录看起来不像ocamlbuild项目(否
“_标记”或“myocamlbuild.ml”文件)。如果你有模块在里面
子目录,
您应该添加选项“-r”或创建一个空的“\u tags”文件。
要仅对某些子目录启用递归遍历,可以
使用
以下“\u标记”文件:
正确:-导线测量
或:导线测量
在00:00:00中生成4个目标(3个已缓存)后编译失败。
此消息清楚地表明问题与线路有关
ecore_evas_init()
编辑

由于@hcarty,原来的问题已经解决了,但是完整的代码不起作用。如果需要,可以找到一个工作版本

ecore\u evas\u init
返回一个
int
隐式表示“左边的表达式返回
单位
”。由于
ecore\u evas\u init()
返回一个整数,因此类型不匹配。您有几个简单的选择:

  • 将现有线路替换为:

    let _ = ecore_evas_init () in
    
    它将忽略返回的值

  • 对返回值执行一些检查:

    assert (ecore_evas_init () = 0); (* Or whatever the "success" status is *)
    
  • 显式忽略结果:

    ignore(ecore_evas_init ());
    
    这大致相当于
    let


  • 在完整的应用程序中,您可能需要更复杂的错误处理,但这应该足以继续本示例应用程序。

    我使用了指令
    ignore
    。你能把它加在你的答案上吗。这对像我这样的初学者是有帮助的。好建议-补充