Skip to content

Commit

Permalink
Move to howto, add more explanations, fix typos
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Sigonneau <benjamin.sigonneau@sandboxaq.com>
  • Loading branch information
benji-sb committed Nov 7, 2024
1 parent 6568af3 commit 74f812c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
1 change: 0 additions & 1 deletion doc/advanced/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ These documents describe some advanced or very specific features of Dune.
ocaml-syntax
variables-artifacts
custom-cmxs
override-default-entrypoint
41 changes: 0 additions & 41 deletions doc/advanced/override-default-entrypoint.rst

This file was deleted.

1 change: 1 addition & 0 deletions doc/howto/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ These guides will help you use Dune's features in your project.
bundle
toplevel
rule-generation
override-default-entrypoint
51 changes: 51 additions & 0 deletions doc/howto/override-default-entrypoint.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
How to override the default entry point
---------------------------------------

In some cases, it may be necessary to override the default main entry point of
an OCaml program. For example, this is the case if you want to let your program
handle argument wildcards expansion on Windows.

Let's consider a trivial "Hello world" program, contained in a ``hello.ml``
file:

.. code:: ocaml
let () = print_endline "Hello, world!"
The default entry point is a C ``main`` function, originally defined in
`runtime/main.c <https://github.com/ocaml/ocaml/blob/trunk/runtime/main.c>`_. It
can be overriden by defining a ``main`` function that will at some point call
the OCaml runtime. Let's write such a minimal example in a ``main.c`` file:

.. code:: C
#include <stdio.h>
#include "caml/misc.h"
#include "caml/mlvalues.h"
#include "caml/sys.h"
#include "caml/callback.h"
/* This is the new entry point */
int main(int argc, char_os **argv)
{
/* Here, we just print a statement */
printf("Do stuff before calling the OCaml runtime\n");
/* Before calling the OCaml runtime */
caml_main(argv);
caml_do_exit(0);
return 0;
}
The :doc:`foreign_stubs </reference/foreign-stubs>` stanza can be leveraged to
compile and link our OCaml program with the new C entry point defined in
``main.c``:

.. code:: dune
(executable
(name hello)
(foreign_stubs
(language c)
(names main)))

0 comments on commit 74f812c

Please sign in to comment.