ocamlocamlfindjs-of-ocaml

How to compile a file that uses the JsooTop module?


I have this in a file named main.ml:

let () = JsooTop.initialize ()

I tried compiling the file using:

ocamlfind ocamlc -package js_of_ocaml -linkpkg -o main.byte main.ml

But this error appears:

File "main.ml", line 1, characters 9-27:
Error: Unbound module JsooTop

It appears that JsooTop is not present on my machine, so I ran opam install js_of_ocaml-toplevel, and tried compiling the file again using:

ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-toplevel -linkpkg -o main.byte main.ml
js_of_ocaml main.byte

But I get warnings:

Warnings from ocamlfind:

findlib: [WARNING] Interface topdirs.cmi occurs in several directories: /usr/lib/ocaml, /usr/lib/ocaml/compiler-libs

Warnings from the js_of_ocaml executable:

There are some missing primitives
Dummy implementations (raising 'Failure' exception) will be used if they are not available at runtime.
You can prevent the generation of dummy implementations with the commandline option '--disable genprim'
Missing primitives provided by +dynlink.js:
  caml_add_debug_info
  caml_dynlink_add_primitive
  caml_dynlink_get_current_libs
  caml_dynlink_lookup_symbol
  caml_dynlink_open_lib
  caml_remove_debug_info
Missing primitives provided by +toplevel.js:
  caml_get_current_environment
  caml_get_section_table
  caml_invoke_traced_function
  caml_realloc_global
  caml_reify_bytecode
  caml_static_alloc
  caml_static_free
  caml_static_release_bytecode
  caml_terminfo_setup

My question is: what is the proper way to compile a file that uses the JsooTop module?


Solution

  • First, make sure that the required OPAM packages are present:

    opam install js_of_ocaml js_of_ocaml-toplevel
    

    To build a program that uses the JsooTop module, compile the file in this way:

    ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-toplevel -linkpkg -o main.byte main.ml
    js_of_ocaml --toplevel --dynlink +dynlink.js +toplevel.js main.byte
    

    Note the inclusion of --toplevel, --dynlink, +dynlink.js, and +toplevel.js. The commands above will produce a JavaScript file named main.js.