Build 7-Zip from Source with GCC or MSVC

Building 7-Zip directly from raw source code allows you to create tailored binaries, optimize performance for specific CPU architectures, and inspect code for security compliance. This guide explains how to compile the official 7-Zip standalone console version (7zz) from scratch using either the Microsoft Visual C++ (MSVC) compiler on Windows or the GNU Compiler Collection (GCC) on Linux.

1. Download and Extract the Source Code

  1. Download the latest source code archive (e.g., 7zXXXX-src.tar.xz or 7zXXXX-src.7z) from the official 7-Zip distribution.
  2. Extract the archive into a dedicated directory:
    tar -xf 7z*-src.tar.xz -C 7zip-src
    cd 7zip-src
    On Windows, extract using an existing 7-Zip installation or file archiver.

2. Building with MSVC on Windows

7-Zip provides NMake-compatible makefiles for the Visual C++ toolchain.

Prerequisites

  • Visual Studio (or Visual Studio Build Tools) with the "Desktop development with C++" workload installed.

Compilation Steps

  1. Open the x64 Native Tools Command Prompt for VS.
  2. Navigate to the Alone2 directory, which builds the unified standalone console version:
    cd CPP\7zip\Bundles\Alone2
  3. Run nmake while specifying the architecture:
    nmake CPU=AMD64
    (For 32-bit x86 builds, use nmake CPU=x86 inside the x86 Native Tools Command Prompt).
  4. Once compilation finishes, the resulting 7zz.exe binary will be located inside the generated output directory:
    b\c\x64\7zz.exe

3. Building with GCC on Linux / POSIX

Modern official 7-Zip releases include native makefiles tailored for GCC on POSIX platforms.

Prerequisites

  • gcc, g++, and make installed (e.g., via sudo apt install build-essential on Debian/Ubuntu).

Compilation Steps

  1. Open a terminal and navigate to the Alone2 bundle directory:
    cd CPP/7zip/Bundles/Alone2
  2. Invoke make using the bundled GCC makefile configuration:
    make -f ../../cmpl_gcc.mak
    To build using parallel jobs for faster compilation, append the -j flag:
    make -f ../../cmpl_gcc.mak -j$(nproc)
  3. After compilation completes, the compiled executable 7zz is located in the target directory:
    ./b/g/7zz

4. Verification

Run the generated binary without arguments to verify that the compile process was successful and to confirm the version number:

  • Windows:
    b\c\x64\7zz.exe
  • Linux:
    ./b/g/7zz

The terminal will display the 7-Zip banner along with the supported compression formats and available command-line switches.