I am looking for an example project with dune and ctype

I am considering learning OCaml, but the quality of the documentation is disappointing. So, I am looking for an example project—it may be Gtk or Ncurses—that demonstrates success in working with C libraries.

The current documentation overwhelms me with pointless details but does not show how to put these things together to create a small project that could evolve into something practical. And to make matters worse the experts who wrote it skip the “obvious” details. It is not obvious to me.

You may look for a project that you are familiar with in this list

Possibly the best was
luv/src/c at master · aantron/luv · GitHub and
Binding C libraries in OCaml with the dune ctypes stanza | Michael Bacarella

It makes no sense to me. I can not find simple examples. Time to give up.

A fairly complete example to get started is provided in Dealing with Foreign Libraries — Dune documentation

While I sympathize with your feelings, please note that the people whose work you are criticising often do this work on their free time and as a service to the community. A more constructive approach would be to 1) report what you find unclear in the appropriate channel (eg the Dune issue tracker) and 2) once you figure out how to do what you wanted, propose a PR upstream to contribute what you learned to the next person.

Cheers,
Nicolas

7 Likes

Hello. For me these resources were useful:

These are “small” examples (with them I was able to create a library to read netcdf using ctypes and dune - still work in progress but working).

Note that Leandro Ostera has a long video working in a possible (I understood that) alternative named bindgen. And in this repo you have a implementation of few raylib functionalities using the standard library (I think there is also a video about the process of creating it).

I hope it helps.

3 Likes

The question was solved in the parallel thread on reddit: the ctypes repository has an examples subdirectory. They can be run directly, e.g. dune exec ./examples/ncurses/foreign/ncurses_cmd.exe, or the dune components can be copied into a fresh project.

2 Likes

If you message me with your email address I will email you a tarball of a small OCaml example program which interfaces with the gtk4 and gettext C libraries and uses dune, which I keep as a reminder to myself of the basics when starting a new project. It includes an example of how an OCaml worker thread can have its continuation execute in the gtk thread (the main program thread). You will need to have some knowledge of gtk follow it in detail. I won’t be able to reply until tomorrow though.

Realising there may be reasons, but does anything stop this being a public Git repo? I find the Unlicense and CC0 combined with a “This is handy for me for foo. YMMV.” README can work very nicely for such demos and otherwise private reminders.

I haven’t put it on github partly because I didn’t think that many would be interested (it’s a bit niche), and partly from inertia - it wasn’t intended for publication so some clean up would be required, some licence applied and so forth. Thank you for your licence suggestion by the way, which as you say seems fairly appropriate for this kind of thing.

The situation seems hopeless because the experts do not understand what a noob does not understand. I was looking for a baby step like this: https://github.com/bigos/ncforeign

Hello @Jacek_Podkanski.

I don’t know what you mean by “The situation seems hopeless because the experts do not understand what a noob does not understand”.

In the first link I gave you Real World OCaml (FFI) there is a small example on how to use ctypes and ctypes-foreign for the ncurses library. Honestly, I think it’s a good starting point.

From my point of view the other link I gave as: simple-ocaml-ctypes-example is also an easy to follow example.

My personal advice is to try working with any of these (or other examples given) and ask more specific questions. We can’t help you if you don’t tell us where you have difficulties. At this point you have written several messages criticising documentation and ‘experts’ but I don’t know anything about what you are trying to do, what you know about OCaml, C, FFI etc… We are trying to help you.

5 Likes