Search code examples
ocamlocamldebugocaml-dune

Creating debug builds of Ocaml code with Jbuilder


I have been reading the tutorials for the Ocaml language and for Jbuilder. The official tutorial indicates that one must compile Ocaml code using the '-g' flag with ocamlc in order to then run ocamldebug.

I cannot find any mention of debug builds on the Jbuilder documentation. The only section that seems close is https://jbuilder.readthedocs.io/en/latest/jbuild.html#ocaml-flags. However, even if I add '-g' as a compilation flag..

(executable
 ((name [REDACTED])
  (public_name [REDACTED])
  (libraries ([REDACTED]))
  (flags (:standard -w -9+27-30-32-40@8
                    -safe-string
                    -linkall
                    -g))
  (modules ([REDACTED]))))

..I still don't seem to get a debug binary:

$ ocamldebug [REDACTED] 
    OCaml Debugger version 4.04.2

(ocd) r
Loading program... [REDACTED] is not a bytecode file.

Am I doing something wrong? If not, what is the recommended way to produce debug builds from jbuilder?


Solution

  • ocamldebug only works with bytecode builds. You're producing native code. To create a bytecode build, you can invoke jbuilder using prog.bc instead of prog.exe.

    Note that this might not be what you're after: you can also debug native programs using plain old gdb, but you'll need to be a bit familiar with the runtime.