Skip to content

Compiling from source

JayDDee edited this page Jan 12, 2025 · 95 revisions

Compiling cpuminer-opt from source code requires a different environment depending on the operating system used. Once the environment has been created the actual compile procedure is the same for all supported CPU architectures and operating systems.

Linux, MacOS & FreeBSD are supported for both x86_64 & ARM64 CPUs. Windows is supported for x86_64 but ARM64 is a work in progress. Other BSDs have not ben tested. All configurations use GCC compiler unless otherwise noted.

Precompiled binaries are available for Windows on x86_64 which can be used instead of compiling from source for those who feel uncomfortable with compiling. There are no binaries available for Windows ARM64 or other operating systems.

There is no seperate section describing BSD setup. Linux can be used as a guide for what packages are required. All packages needed by FreeBSD are avaiable using pkg. Other BDSs have not been tested.

The short version for most cases is to install the GNU/Make development environment including GCC and the additional libraries needed to build cpuminer-opt.

Linux build environment

The following is based on Ubuntu and also applies to other Debian based Linux distributions. Fedora and other distributions have different package managers and may have slightly different package naming. Some packages may have separate runtime and development versions of the same package. Ensure the development or headers versions are installed.

Make sure you have the basic development packages installed. Here is a good start:

http://askubuntu.com/questions/457526/how-to-install-cpuminer-in-ubuntu

Install any additional dependencies needed by cpuminer-opt. The list below are some of the ones that may not be in the default install and need to be installed manually. There may be others, read the compiler error messages, they will give a clue as to the missing package.

The following command should install everything you need on Debian based distributions.

$ sudo apt-get install build-essential gcc automake libcurl4-openssl-dev libjansson-dev libgmp-dev zlib1g-dev git

cpuminer-opt can now be compiled on Linux, see Compiling cpuminer-opt below.

Windows build environment

cpuminer-top is compiled on Windows using MSys2 and MinGW64. This installs a Linux-like environment where cpuminer-opt can be compiled and run on a Windows computer. MS Visual Studio is not supported. CPUs based on the x86_64 architecture (Intel & AMD) require Windows-7 64 bit and newer. ARM64 CPUs require Windows-11 or newer. The MSys2 file system is slow affecting build time, however, this does affect hashrate of cpuminer-opt.

Windows on ARM64 is unstable when compiled with Clang. GCC is not yet available for MinGW on ARM64.

Install MSys2 MinGW64

Download and install the latest version of msys2-x86_64 from https://www.msys2.org/. The x86_64 installer will install MSys2 on ARM64 CPUs as well as x86_64.

Update packages and install the mingw toolchain but leave the MSYS2 MSYS window open at the end to install more packages in the next step.

Install additional packages required for compiling cpuminer-opt.

x86_64 packages

$ pacman -S gcc autotools libcurl-devel mingw-w64-x86_64-curl gmp-devel jansson-devel zlib-devel

ARM64 packages

$ pacman -S mingw-w64-clang-aarch64-clang mingw-w64-clang-aarch64-autotools mingw-w64-clang-aarch64-curl mingw-w64-clang-aarch64-gmp mingw-w64-clang-aarch64-jansson mingw-w64-clang-aarch64-zlib

Open the apropriate MSys2 terminal for your architecture from the start menu. On x86_64 open MINGW64 to compile with GCC, on ARM64 open CLANGARM64 to compile with Clang.

Ensure the correct one is used. GCC is recommended but is only avalable for x86_64 at this time. On ARM64 the only choice is Clang which has problems described in errata below.

If Windows tools are preferred cpuminer-opt can be downloaded to the Windows Documents folder and accessed from MSYS2 terminal using the path /c/users/[USER]/documents. Similarely the MSYS2 home directory can be accessed from Windows with C:\msys2\home\[USER]. Notepad++ and 7zip are useful Windows programs with good support for Linux file formats.

Windows integration, optional

cpuminer-opt must normally be run from the MSys command terminal. If desired it can be run from a Windows command prompt or Powershell using DOS syntax by adding MSYS2 to the system Path environment variable. FYI: https://stackoverflow.com/questions/6951938/libgmp-10-dll-is-missing

cpuminer-opt can now be compiled on Windows, see Compiling cpuminer-opt below.

Errata

  • CPU temperature and frequency reports are not supported on Windows.
  • Windows on ARM64 can only be compiled with Clang at this time resulting in an unstable build that may crash.
  • Windows on ARM64 may result in cpuminer-opt segfault when starting up. Add -DARM_WIN_HACK to CFLAGS to workaround it. This workaround may or may not work and may or may not be necessary.
  • Windows on ARM does not display CPU features correctly.

Apple MacOS build environment

Apple MacOS is supported on ARM64 and older MACs with Intel x86_64 CPUs. This procedure is only for Apple MAC PCs that use MacOS. iPads & iPhones use IOS which isn't supported and will not work.

Setting up the build environment on MacOS does not require an AppleID or need anything from the App store. Everything is done from the Terminal. MacOS uses Clang compiler by default which works on ARM64 but has problems on MACs with x86_64 which need to be compiled with GCC.

Install developer command line tools

If Xcode is installed this is probably not necessary. Using Xcode to build cpuminer-opt has not been tested. Use xcode-select from the terminal to install the default tools withou the full Xcode.

% xcode-select --install

Homebrew package manager

Install Homebrew from https://brew.sh/. Use Homebrew to install missing packages.

Homebrew may compile some packages from source which can take a long time.

ARM64 packages

Install these packages on newer Apple computers with M series ARM64 architecture CPU. Clang is installed by default. Compiling with GCC doesn't work on Apple ARM64.

% brew install aarch64-elf-binutils autoconf automake ca-certificates gettext gmp isl jansson libmpc libunistring lz4 m4 mpfr pcre2 zstd

cpuminer-opt can now be compiled on MacOS ARM64 with the default Clang compiler. See Compiling cpuminer-opt below.

x86_64 packages

Install these packages on older Apple computers with Intel x86_64 architecture CPU. x86_64 CPUs must be compiled using GCC, Clang doesn't work.

% brew install x86_64-elf-binutils x86_64-elf-gcc gcc autoconf automake ca-certificates gettext gmp isl jansson libmpc libunistring lz4 m4 mpfr pcre2 zstd

Change the default compiler to GCC.

There are many tutorials available on how to switch compilers, some methods are temporary and others are more persistent. The following will work for the existing terminal session.

$ export CC=gcc

$ export CXX=g++

cpuminer-opt can now be compiled on MacOS x86_64 using GCC. See Compiling cpuminer-opt below.

Errata

  • CPU temperature and frequency reports are not supported on MacOS.
  • MacOS on ARM64 does not display CPU features correctly.

Compiling cpuminer-opt

At this point it's assumed the appropriate development environment has been setup for the target operating system and CPU architecture and a command line terminal is open and ready to compile.

Download cpuminer-opt only from the official JayDDee github repository: https://github.com/JayDDee/cpuminer-opt/ Download the source code for latest release or clone the repository. Do not download zip source files because file attributes are not preserved by zip. Download with a browser or use wget (if installed) from the command line terminal.

$ wget https://github.com/JayDDee/cpuminer-opt/archive/refs/tags/vX.Y.tar.gz

Replace X & Y with the latest release.

Extract the source code.

$ tar xvzf cpuminer-opt-X.Y.tar.gz

Alternatively the latest version can be cloned from git if installed.

$ git clone https://github.com/JayDDee/cpuminer-opt.git

Build cpuminer-opt

It is recomended to Build with default options, this will usually produce the best results. Enter the cpuminer-opt directory and run:

$ ./build.sh

or execute the commands manually to make changes to options:

$ ./autogen.sh

$ CFLAGS="-O3 -march=native -Wall" ./configure --with-curl

$ make -j N

N is the number of threads for the compiler to use. Use -j $(nproc) to use all CPU cores.

Some newer CPU architectures may not be defined for -march=native in older versions of the compiler and may not recognize new features using the default build script. It is often possible to add the missing features to create an optimum build by manually specifying options.

Many examples can be found in file build-allarch.sh for x86_64 CPUs, and in armbuild-all.sh for ARM64 CPUs.

Windows on ARM64 may crash with a segfault. See Windows section above. YMMV

Start mining.

$ ./cpuminer -a algo -o url -u username -p password

On Windows the executable file has a .exe extension.

See https://github.com/JayDDee/cpuminer-opt/wiki/CLI-and-config-options for more mining options.

Building the Windows x86_64 binary package.

This procedure is only used to build the Windows binary release package for x86_64 and is done from a Linux environment. It is not intended for users and is not supported. It is documented primarily for purposes of transparency. All binary files included in the package are created from cpuminer-opt source code, copied from the host operating system, or created from source code as described below.

These instructions were written specifically for Ubuntu-20.04.

A new user is created specifically for cross compiling. It keeps all mingw stuff contained and isolated from the rest of the system.

Step by step...

Install packages

Install necessary packages from the distribution's repositories. Refer to Linux compile instructions and install required packages.

Additionally, install mingw-w64.

$ sudo apt-get install mingw-w64 libz-mingw-w64-dev

Create a local library

Create a directory for libraries that need to be compiled in the next stept step. The Suggested location is $HOME/usr/lib/

$ mkdir $HOME/usr/lib

Build libraries

The following procedure assumes Ubuntu-20.04 as the host and is the only configuration guaranteed to work. Download and build other packages for mingw that don't have a mingw64 version available in the repositories.

Download the following source code packages from their respective and respected download locations,copy them to $HOME/usr/lib/ and uncompress them.

curl v7.68.0: https://github.com/curl/curl/releases

gmp v6.2.0: https://gmplib.org/download/gmp/

Download the same versions as currently installed on the host. The versions listed above are for Ubuntu 20.04.

Run the following commands or follow the supplied instructions. Do not run "make install" unless using /usr/lib, which isn't recommended.

Some instructions insist on running "make check". If make check fails it may still work, YMMV.

curl:

$ ./configure --with-winssl --with-winidn --host=x86_64-w64-mingw32

$ make

gmp:

$ ./configure --host=x86_64-w64-mingw32

$ make

Tweak the environment.

Many of the following steps are performed automatically by winbuild-cross.sh. This script is used to build the Windows release package. Although it was not written for users it can help simplify compiling. If necessary edit winbuild-cross.sh to correct any directory paths to match your environment.

This step is required everytime user logs in or the commands can be added to .bashrc.

Define some local variables to point to the local library.

$ export LOCAL_LIB="$HOME/usr/lib"

$ export CONFIGURE_ARGS="--with-curl=$LOCAL_LIB/curl --host=x86_64-w64-mingw32"

Adjust for gcc version:

$ export GCC_MINGW_LIB="/usr/lib/gcc/x86_64-w64-mingw32/9.3-win32"

Create a release directory and copy some dll files previously built. This can be done outside of cpuminer-opt and only needs to be done once. If the release directory is in cpuminer-opt directory it needs to be recreated every time a source package is decompressed.

$ mkdir release

$ cp /usr/x86_64-w64-mingw32/lib/zlib1.dll release/

$ cp /usr/x86_64-w64-mingw32/lib/libwinpthread-1.dll release/

$ cp $GCC_MINGW_LIB/libstdc++-6.dll release/

$ cp $GCC_MINGW_LIB/libgcc_s_seh-1.dll release/

$ cp $LOCAL_LIB/curl/lib/.libs/libcurl-4.dll release/

Download cpuminer-opt

The following steps need to be done every time a new source package is opened.

Download the latest source code package of cpumuner-opt to your desired location. Downloading the tar.gz file is preferred as it preserves file permissions, specifically execute permission.

https://github.com/JayDDee/cpuminer-opt/releases

Decompress and change to the cpuminer-opt directory.

Compile

Create a link to the locally compiled version of gmp.h

$ ln -s $LOCAL_LIB/gmp-version/gmp.h ./gmp.h

$ ./autogen.sh

Configure the compiler for the CPU architecture of the host machine:

$ CFLAGS="-O3 -march=native -Wall" ./configure $CONFIGURE_ARGS

or cross compile for a specific CPU architecture:

$ CFLAGS="-O3 -march=znver1 -Wall" ./configure $CONFIGURE_ARGS

This will compile for AMD Ryzen.

Compile more generically for a set of specific CPU features:

$ CFLAGS="-O3 -maes -msse4.2 -Wall" ./configure $CONFIGURE_ARGS

This will compile for an older CPU that does not have AVX.

Run make:

$ make -j n

Copy cpuminer.exe to the previously created release directory, compress and copy the release directory to a Windows system and run cpuminer.exe from the command line.

Run cpuminer

In a command window or Powershell change directories to the unzipped release folder. To get a list of all options:

$ cpuminer.exe --help

Command options are specific to where you mine. Refer to the pool's instructions on how to set them.