How to Cross-Compile UnRAR for Another Architecture
Cross-compiling unrar allows you to generate a
lightweight, standalone extraction binary for target architectures like
ARM, MIPS, or RISC-V using a more powerful host machine. This guide
walks through obtaining the official source code, preparing your
cross-toolchain, overriding the build configuration, and verifying the
final binary.
Prerequisites
Before beginning, ensure your host system has the standard build
utilities and the cross-compiler toolchain for your target architecture
installed. For example, to target 64-bit ARM (aarch64):
sudo apt update
sudo apt install build-essential gcc-aarch64-linux-gnu g++-aarch64-linux-gnuAdjust the package name depending on your target platform (e.g.,
arm-linux-gnueabihf- for 32-bit ARM or
mips-linux-gnu- for MIPS).
Step 1: Download the UnRAR Source Code
Download the portable source code archive directly from RARLAB and extract it:
wget https://www.rarlab.com/rar/unrarsrc-6.2.12.tar.gz
tar -xzf unrarsrc-6.2.12.tar.gz
cd unrar(Note: Replace the version number with the current release as needed.)
Step 2: Understand the Makefile Structure
UnRAR does not use an autotools configure script or
CMake. Instead, it relies on a standard makefile located in
the root directory.
Key variables within this makefile include:
CXX: The C++ compiler executable.STRIP: The strip utility used to reduce binary size.AR: The archive tool (used if buildinglibunrar).CXXFLAGS: Compiler flags.LDFLAGS: Linker flags.
Step 3: Execute the Cross-Compilation
To cross-compile, pass your cross-compiler toolchain prefixes
directly to the make command without altering the source
files.
Dynamic Binary
For standard cross-compilation linking against the target system's dynamic C runtime:
make -f makefile \
CXX=aarch64-linux-gnu-g++ \
STRIP=aarch64-linux-gnu-strip \
AR=aarch64-linux-gnu-arStatically Linked Binary (Recommended for Embedded Systems)
If the target system may lack compatible shared C++ standard libraries, build a statically linked binary:
make -f makefile \
CXX=aarch64-linux-gnu-g++ \
STRIP=aarch64-linux-gnu-strip \
AR=aarch64-linux-gnu-ar \
LDFLAGS="-static"Step 4: Verify the Compiled Binary
After the build completes, confirm that the generated executable matches your target architecture rather than your host architecture:
file unrarExpected output for an aarch64 target:
unrar: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, stripped
Step 5: Deploy to Target
Transfer the resulting unrar binary to your target
environment using scp, USB, or network storage, and set
executable permissions:
chmod +x unrar
./unrar