Re: a simple example of compiler-libs.toplevel?

Kenichi Asai <[email protected]> Wed, 15 Apr 2026 21:45:47 +0900
Newsgroups gmane.comp.lang.caml.inria
Message-ID <[email protected]>
Five years ago, I asked a question to this mailing list on how to
create a copy of the OCaml toplevel using compiler-libs (so that I can
customize it).  I am trying it again.  The thread is here:

https://inbox.vuxu.org/caml-list/CANhEzE5tOqq7JXA47tmkQcnF6Bz70eU8jPBT+=+g2EwQ7cNf6Q@mail.gmail.com/T/

What I did is to copy the necessary files from the OCaml source (I am
working on OCaml 4.14.2.):

topstart.ml, topmain.ml, toploop.ml, topeval.ml, topdirs.ml
topcommon.ml, trace.ml, genprintval.ml

rename them to my_topstart.ml, my_topmain.ml, etc, rename all the
module references to the new ones (My_toploop instead of Toploop, etc.)
and compiled them with compiler-libs.  See the toplevel1 directory of:

https://github.com/kenichi-asai/toplevel-experiment

However, when I compile them (by "make"), and execute the binary, I get:

$ ./toplevel
OCaml version 4.14.2
Enter #help;; for help.

# let a = 3;;
Error: Reference to undefined global `Toploop'
#

Why does this happen?

When I search for "Toploop" in the OCaml source outside the toplevel
directory, I found lambda/translmod.ml mentions it:

let toploop_ident = Ident.create_persistent "Toploop"

Does this mean the toplevel must be given by the Toploop module
instead of My_toploop?

As a second attempt, I created the toplevel2 directory of the above
repository, where I did not rename files to "my_*" but used the
original names.  When I compiled them and execute the binary, I get
segmentation fault (!):

$ ./toplevel
OCaml version 4.14.2
Enter #help;; for help.

# let a = 3;;
Segmentation fault
$

What's wrong?  How can I create a copy of the OCaml toplevel using
compiler-libs?  Any help appreciated.

Another question.  I also want to use dune to compile, instead of
OCamlMakefile.  I provided dune and dune_project files in the
repository, but when I execute dune build, I obtain the following.
What am I doing wrong?

$ dune build
File "dune", line 2, characters 7-18:
2 |  (name my_topstart)
           ^^^^^^^^^^^
Undefined symbols for architecture x86_64:
  "_caml_get_current_environment", referenced from:
      _camlDune__exe__My_topmain__fun_2483 in dune__exe__My_topmain.o
      _camlDune__exe__My_topmain__1 in dune__exe__My_topmain.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
File "caml_startup", line 1:
Error: Error during linking (exit code 1)

Thank you in advance!

-- 
Kenichi Asai