LinuxCommandLibrary

zig

Compile Zig code

TLDR

Compile the project in the current directory

$ zig build
copy

Compile and run the project in the current directory
$ zig build run
copy

Initialize a zig build project with library and executable
$ zig init
copy

Create and run a test build
$ zig test [path/to/file.zig]
copy

Cross compile, build and run a project for x86_64 architecture and windows operating system
$ zig build run -fwine -Dtarget=x86_64-windows
copy

Reformat Zig source into canonical form
$ zig fmt [path/to/file.zig]
copy

Translate a C file to zig
$ zig translate-c -lc [path/to/file.c]
copy

Use Zig as a drop-in C++ compiler
$ zig c++ [path/to/file.cpp]
copy

SYNOPSIS

zig <subcommand> [options] [arguments]

Common invocations:
zig build [options]
zig run <file> [args]
zig test <file>
zig cc <source.c> [options]

PARAMETERS

build
    Compiles and runs a project's build.zig file, executing defined build steps.

run
    Compiles and immediately executes a single Zig source file.

test
    Compiles and runs all tests defined within a Zig source file or project.

init-exe
    Initializes a new executable Zig project with a basic structure.

init-lib
    Initializes a new library Zig project with a basic structure.

cc
    Acts as a C compiler frontend, allowing compilation of C/C++ files using Zig's compiler infrastructure.

fmt
    Formats Zig source code according to official style guidelines.

translate-c
    Translates C header files into Zig equivalent declarations.

version
    Displays the installed Zig compiler version.

--help
    Displays general help or help for a specific subcommand.

--verbose
    Enables verbose output for compilation and build processes.

--release-fast
    Compiles with optimizations for speed, suitable for production.

--release-safe
    Compiles with optimizations and safety checks enabled (e.g., bounds checks).

--release-small
    Compiles with optimizations for minimum binary size.

DESCRIPTION

The zig command is the primary interface for the Zig programming language. It acts as both a compiler and a versatile build system. Zig is a general-purpose, low-level programming language designed for robustness, optimality, and explicit control. The zig command allows users to compile Zig source files, cross-compile for various targets, run tests, and manage project dependencies. It integrates a powerful build system, often replacing traditional build tools like Make or CMake, by using a build.zig file written in Zig itself. This enables complex build logic to be expressed directly within the language, facilitating reproducible builds and simplified project management. Additionally, zig functions as a C/C++/Objective-C compiler, leveraging LLVM, which makes it a powerful drop-in replacement for gcc or clang in many contexts, offering features like link-time optimization (LTO) and advanced error checking.

CAVEATS

The zig command is not a standard, pre-installed Linux utility; it must be downloaded and installed separately. As the Zig language and its compiler are under active development, specific commands, options, and behaviors may evolve rapidly between versions, requiring users to consult the official documentation for the version they are using.

CROSS-COMPILATION CAPABILITIES

One of zig's most praised features is its exceptional support for cross-compilation. It can effortlessly compile code for a vast array of target architectures and operating systems (e.g., ARM, WebAssembly, Windows, macOS, Linux) directly from a single development environment, often without the need to install separate toolchains. This simplifies the development and deployment of multi-platform and embedded applications significantly.

INTEGRATED BUILD SYSTEM

Unlike many programming languages that rely on external build systems (like Make, CMake, or Cargo), Zig features an integrated and extensible build system defined by a build.zig file. This file is itself a Zig program, allowing developers to write complex and custom build logic using the full power of the Zig language, including compile-time execution. This approach fosters reproducible builds and streamlines project setup and dependency management.

HISTORY

Zig was created by Andrew Kelley, with initial development beginning around 2015. It emerged from a desire for a systems programming language that provided explicit control and simplicity, without hidden control flow or memory allocations. It gained significant attention for its innovative approach to compile-time metaprogramming, error handling, and its powerful C/C++ interop capabilities. The project is open-source and community-driven, with development primarily managed through GitHub, characterized by rapid iteration and an engaged user base.

SEE ALSO

gcc(1), clang(1), make(1), cmake(1)

Copied to clipboard