Skip to main content
The Z3 OCaml bindings provide access to Z3’s SMT solving capabilities from OCaml. The bindings support both static and dynamic linking and work with all OCaml linkers.

Requirements

OCaml

  • OCaml 4.09.0 or later
  • ocamlfind (findlib)
  • opam (recommended)

Build Tools

  • C/C++ compiler
  • Python 3.x or CMake
  • Git

Installation Methods

Build Z3 with OCaml bindings from source.
1

Clone Z3 Repository

2

Build with Python

The --staticlib flag creates a static library, making binaries self-contained.
3

Install with ocamlfind

This installs:
  • OCaml modules: z3.cmi, z3.cmo, z3.cmx, etc.
  • Static library: libz3-static.a
  • Libraries: z3ml.{a,cma,cmxa,cmxs}

Method 2: OPAM (When Available)

The Z3 OCaml package availability in OPAM may vary. Check the opam repository for the latest version.
If the package is not available or outdated, use Method 1.

Build Artifacts

The build process creates several files:

OCaml Modules

files
Core Z3 module with type definitions and main API
files
Z3 enumeration types
files
Native bindings to Z3 C API

Libraries

library
Native code static library for ocamlopt
library
Bytecode library for ocamlc
library
Standalone shared library for dynamic loading with Dynlink
library
C stubs archive
library
Shared object for OCaml runtime (bytecode)
library
Z3 library itself (when using --staticlib)

Compiling OCaml Programs

The easiest way to compile programs using Z3:
The -thread flag is required for the Z3 bindings.

Static Linking

With --staticlib, native binaries have no Z3 dependency:

Custom Bytecode (Self-Contained)

Create a portable bytecode executable:
The resulting binary is self-contained (no DLL dependencies).

Manual Compilation

Without ocamlfind:

Using in OCaml Toplevel

Load Z3 in the OCaml REPL:

OCaml Scripts

Create executable OCaml scripts:
script.ml
Make it executable:
The z3ml.cmxs file can be dynamically loaded:
This is useful for plugins and dynamic code loading.

Verification

Test your installation:
test.ml
Compile and run:
Expected output:

Platform-Specific Notes

Most distributions work out of the box:
If using dynamic linking, ensure library path is set:

Troubleshooting

Problem: ocamlfind can’t locate Z3.Solution:
  • Verify installation: ocamlfind query z3
  • Reinstall: ocamlfind remove z3 then reinstall
  • Check OPAM switch: opam switch
Problem: Undefined references to Z3 symbols.Solution:
  • Ensure -thread flag is used
  • With dynamic linking, set LD_LIBRARY_PATH
  • Use --staticlib when building for self-contained binaries
Problem: dllz3ml.so not found when running bytecode.Solution:
  • Install properly with ocamlfind (it handles stublibs)
  • Or use -custom flag for self-contained bytecode
  • Check: ocamlfind query -format '%d/stublibs' z3
Problem: OCaml module compiled with different Z3 version.Solution:
  • Rebuild Z3 from source
  • Clean build directory: make clean before rebuilding
  • Reinstall with ocamlfind

Advanced: Manual Installation Paths

If not using ocamlfind:

Docker Setup

Use Docker for isolated environment:
Dockerfile

Next Steps

Getting Started

Learn the basics with examples

Examples

Example programs on GitHub

API Documentation

OCaml API reference

Build Instructions

Detailed build guide