I try to bind C enums with Ctypes but I am not able to make it work. Here is an example :
In the GLib-2 library, threre is this enumeration :
typedef enum
{
G_BOOKMARK_FILE_ERROR_INVALID_URI,
G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
G_BOOKMARK_FILE_ERROR_READ,
G_BOOKMARK_FILE_ERROR_UNKNOWN_ENCODING,
G_BOOKMARK_FILE_ERROR_WRITE,
G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND
} GBookmarkFileError;
So I create in a directory named enum_ctypes the following files :
enum_ctypes/
|__ _oasis
|__ lib/
|__ MyEnum.ml
The content of the _oasis file :
OASISFormat: 0.4
Name: enum_ctypes
Version: 0.0.0
Synopsis: test enum ctypes
Authors: cedlemo
License: GPL-3
Plugins: META (0.4), StdFiles (0.4), DevFiles (0.4)
BuildTools: ocamlbuild
Library "MyEnum"
Path: lib
Modules: MyEnum
BuildDepends: ctypes, ctypes.foreign
CCLib: -Wl,-no-as-needed -lgirepository-1.0 -lgobject-2.0 -lglib-2.0
CCOpt: -O2 -Wall -Wextra -Wno-unused-parameter -pthread
-I/usr/include/gobject-introspection-1.0
-I/usr/lib/libffi-3.2.1/include
-I/usr/include/glib-2.0
-I/usr/lib/glib-2.0/include
And the content of the MyEnum.ml file :
open Ctypes
open Foreign
let _invalid_uri = constant "G_BOOKMARKFILEERROR_INVALID_URI" uint32_t
and _invalid_value = constant "G_BOOKMARKFILEERROR_INVALID_VALUE" uint32_t
and _app_not_registered = constant "G_BOOKMARKFILEERROR_APP_NOT_REGISTERED" uint32_t
and _uri_not_found = constant "G_BOOKMARKFILEERROR_URI_NOT_FOUND" uint32_t
and _read = constant "G_BOOKMARKFILEERROR_READ" uint32_t
and _unknown_encoding = constant "G_BOOKMARKFILEERROR_UNKNOWN_ENCODING" uint32_t
and _write = constant "G_BOOKMARKFILEERROR_WRITE" uint32_t
and _file_not_found = constant "G_BOOKMARKFILEERROR_FILE_NOT_FOUND" uint32_t
let bookmarkfileerror : [`Invalid_uri|`Invalid_value|`App_not_registered|`Uri_not_found|`Read|`Unknown_encoding|`Write|`File_not_found] typ = enum "bookmarkfileerror" [
`Invalid_uri, _invalid_uri;
`Invalid_value, _invalid_value;
`App_not_registered, _app_not_registered;
`Uri_not_found, _uri_not_found;
`Read, _read;
`Unknown_encoding, _unknown_encoding;
`Write, _write;
`File_not_found, _file_not_found
] ~unexpected:(fun i -> `Unexpected i)
When I try to build this with the following commands in the enum_ctypes directory :
oasis setup -setup-update dynamic
./configure
make
I have the following error message :
File "lib/MyEnum.ml", line 4, characters 19-27:
Error: Unbound value constant
How can I solve this problem ?