ocamlz3opamsat-solversoasis

Z3 bindings on ocaml


I am currently using ocaml 4.06.0 and I am trying to use the Z3 sat solver. I am using opam's oasis to compile the files (which is building everything successfully). However, when I run the native code produced I am getting the following error: error while loading shared libraries: libz3.so. I tried reinstalling the z3 package but the error still persists. Can anyone help me solve this please because I have no idea what else to try?


Solution

  • Here is what I did just now to install z3 under Ubuntu 18.04.1:

    $ opam depext conf-gmp.1
    $ opam depext conf-m4.1
    

    These installed gmp and m4 outside of opam. Pretty impressive.

    $ opam install z3
    

    Now the z3 library is installed so you can use it from OCaml code. But there are no executables installed (that I can find).

    $ export LD_LIBRARY_PATH=~/.opam/4.06.0/lib/z3
    $ ocaml -I ~/.opam/4.06.0/lib/z3
            OCaml version 4.06.0
    
    # #load "nums.cma";;
    # #load "z3ml.cma";;
    # let ctx = Z3.mk_context [];;
    val ctx : Z3.context = <abstr>
    

    The setting of LD_LIBRARY_PATH is what makes it possible to find libz3.so.

    This is as far as I got for now. Maybe this will be helpful.

    Update

    Here is how I compiled and linked a test program.

    $ export LD_LIBRARY_PATH=~/.opam/4.06.0/lib/z3
    $ cat tz3.ml
    let context = Z3.mk_context []
    let solver = Z3.Solver.mk_solver context None
    
    let xsy = Z3.Symbol.mk_string context "x"
    let x = Z3.Boolean.mk_const context xsy
    
    let () = Z3.Solver.add solver [x]
    
    let main () =
        match Z3.Solver.check solver [] with
        | UNSATISFIABLE -> Printf.printf "unsat\n"
        | UNKNOWN -> Printf.printf "unknown"
        | SATISFIABLE ->
            match Z3.Solver.get_model solver with
            | None -> ()
            | Some model ->
                Printf.printf "%s\n"
                    (Z3.Model.to_string model)
    
    let () = main ()
    
    $ ocamlopt -I ~/.opam/4.06.0/lib/z3 -o tz3 \
         nums.cmxa z3ml.cmxa tz3.ml
    
    $ ./tz3
    (define-fun x () Bool
      true)
    $ unset LD_LIBRARY_PATH
    $ ./tz3
    ./tz3: error while loading shared libraries: libz3.so:
     cannot open shared object file: No such file or directory
    

    It works--i.e., it says that the trivial formula x can be satisfied by making x be true.

    Note: initially I thought the setting of LD_LIBRARY_PATH wasn't necessary here. But in later testing I've found that it is necessary. So that is probably the key to your problem.

    It's a little cumbersome and error prone to set LD_LIBRARY_PATH for running your programs. It's good enough for personal testing, but probably not for any kind of wider deployment. There are ways to set the search path for shared libraries at link time.

    I hope this helps.