Skip to main content
The Z3 Go bindings provide a comprehensive interface to Z3’s C API using CGO. The bindings are included in the Z3 source repository and must be built from source.

Requirements

Go

  • Go 1.20 or later
  • CGO enabled (default)

Build Tools

  • C/C++ compiler (gcc, clang, or MSVC)
  • CMake 3.4+ or Python 3.x
  • Git

Build Z3 with Go Bindings

The Go bindings must be built as part of the Z3 library.
1

Clone Z3 Repository

2

Configure with CMake

Add -DCMAKE_BUILD_TYPE=Release for optimized builds.
3

Build Z3

4

Verify Build

The build creates:
  • libz3.so / libz3.dylib / z3.dll - Z3 shared library
  • Go bindings in src/api/go/

Method 2: Python Build System

1

Clone and Configure

2

Build

Set Up Go Environment

After building Z3, configure your Go environment to use the bindings.

Linux / macOS

Add to ~/.bashrc or ~/.zshrc for permanent configuration:
~/.bashrc

macOS Specific

On macOS, use DYLD_LIBRARY_PATH instead:

Windows

For PowerShell:

Using Go Modules

The Z3 Go bindings use Go modules. Your project should reference the Z3 repository:

go.mod

go.mod
The replace directive points to your local Z3 build since the Go bindings are not published to a Go module registry.

Using in Your Code

main.go

Verification

Test your installation:
test.go
Run it:
Expected output:

Installation Options

System-Wide Installation

To install Z3 system-wide:
After system installation, you may not need LD_LIBRARY_PATH:

Static Linking

For static linking, build Z3 as a static library:
Then link statically:

Troubleshooting

Problem: Linker can’t find Z3 library.Solution:
  • Verify CGO_LDFLAGS includes -L/path/to/build -lz3
  • Check that libz3.so exists in the build directory
  • On Linux: Run sudo ldconfig after system install
  • On Windows: Ensure z3.dll is in PATH
Problem: Compiler can’t find Z3 headers.Solution:
  • Verify CGO_CFLAGS includes -I/path/to/z3/src/api
  • Check that z3.h exists in src/api/z3.h
Problem: Go can’t find the Z3 package.Solution:
  • Check your go.mod has the correct replace directive
  • Verify the path points to z3/src/api/go (not just z3)
  • Run go mod tidy
Problem: error while loading shared libraries: libz3.soSolution:
  • Set LD_LIBRARY_PATH (Linux) or DYLD_LIBRARY_PATH (macOS)
  • Or install Z3 system-wide and run ldconfig
  • On Windows: Ensure directory with z3.dll is in PATH
Problem: CGO is disabled.Solution:
Problem: XCode command line tools or compiler missing.Solution:

Docker Setup

Use Docker for isolated development:
Dockerfile

Platform-Specific Notes

Most Linux distributions work out of the box with gcc/g++.Install dependencies:

Next Steps

Getting Started

Learn the basics with examples

API Reference

Complete API documentation

Examples

Example programs on GitHub

Build Guide

Detailed build instructions