I am writing an OCaml library that has some initialization code that needs to be run only once during the life of the program using the library (and store some state that will be maintained throughout the life of the program, but will be used only inside the library itself) and some cleanup code that should only run as a program that uses the exit from the library.
If relevant, my library consists of two parts: an interface with a low-level C library and some higher-level materials to simplify programming. Can I do what I need somewhere in C? Ideally, my users would not care how it was implemented, they never saw the C bit.
In Python, I would do this by running the import code, but OCaml open doesn't actually run anything, it just lands the module namespace and then Python atexit , but I can't find the Ocaml equivalent.
One approach that I have considered is structuring my library as a “framework,” but I don't think it's important enough to justify such a redesigned approach. Thanks!
UPDATE : OK it turned out - I think. I use C code to handle cleanup on exit, and I have worked a bit with the code, so there is a pointer to the global state on the C side
It looks like there is now in my library
let global_env = env_create ()
And when it is open 'd main program, it starts ... But how?
Gaius source share