Dear Paul,

You just need to link in ocamltoplevel.cma by adding it to the command-line

  ocamlc -I +compiler-libs ocamltoplevel.cma -o test.out test.ml

(the compiler will be able to find this file thanks to the "-I +compiler-libs" flag).

Cheers,
Nicolas

On Mon, Apr 10, 2017 at 2:55 PM, <paul.lachat@edu.univ-fcomte.fr> wrote:
Hello,

I would like to use functions of the toplevel (https://github.com/ocaml/ocaml/blob/trunk/toplevel/toploop.ml)

I've tried to compile this code just to understand how I can link the toploop file with my program :
____
test.ml :

let _ =
    let lb = Lexing.from_string "let x = 10;;" in
    let ppf = Format.std_formatter in
    try
    List.iter
      (fun phr ->
        if not (Toploop.execute_phrase false ppf phr) then raise Exit)
      (!Toploop.parse_use_file lb)
    with
    | Exit -> ()
    | x -> Errors.report_error ppf x

____

I've test :

[-] ocamlc -I +compiler-libs -o test.out test.ml

[*] File "test.ml", line 1:
    Error: Error while linking test.cmo:
    Reference to undefined global `Toploop'

____

So I create test.cmo with :
[-] ocamlc -I +compiler-libs -c test.ml, but I don't know where to find "toploop.cmo".

I use Ocaml 4.03.0 with Opam.
In ".opam/4.03.0/lib/ocaml/compiler-libs/" I have "toploop.cmi", "toploop.cmti" and "toploop.cmt", but no "toploop.cmo" to link to the program.

I've seen this file ".opam/4.03.0/lib/ocaml/compiler-libs/ocamltoplevel.cma", but I don't know if it's usefull.

I think it's possible to use Toploop. In this post, https://stackoverflow.com/questions/33291754/eval-a-string-in-ocaml, they seems to have find a way to compile a program who use Toploop. But I would like, if possible, to avoid dependecys like ocamlbuild.

____

So, I would like to know how I can compile this program ?