Skip to content

Compiling from source

JayDDee edited this page Nov 17, 2023 · 56 revisions

Building on Linux

Building on linux prerequisites:

It is assumed users know how to install packages on their system and be able to compile standard source packages. This is basic Linux and beyond the scope of cpuminer-opt. Regardless compiling is trivial if you follow the instructions.

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 such as Ubuntu. Fedora and other distributions may have similar but different package names. As of v23.8 OpenSSL (libssl-dev) is no longer reqwuired.

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

Download cpuminer-opt

Download the source code for the latest release from the official repository.

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

Extract the source code.

$ tar xvzf cpuminer-opt-x.y.z.tar.gz

Alternatively it can be cloned from git.

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

Build cpuminer-opt

It is recomended to Build with default options, this will usuallly produce the best results.

$ ./build.sh

or

$ ./autogen.sh

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

$ make -j N

N is the number of threads for the compiler to use.

Some newer CPU architectures may not be defined for "-march=native" in older versions of gcc and can't be compiled using the default build script. It is often possible to compile an optimum build by manually specifying options.

Ryzen 3000 uses "-march=znver2" natively but znver1 also works.

Ryzen 5000 uses "-march=znver3" natively in gcc-11 and newer. For olders versions of gcc use "-march=znver2 -mvaes"

Ryzen 7000 uses "-march-znver4" but is not yet supported as of gcc-12. It can be compiled with gcc-11 by using "-march=znver3 -mavx512f -mavx512dq -mavx512bw -mavx512vl"

Intel Alderlake removed support for AVX512 and is therefore not compatible with Rocketlake or Icelake. "march=alderlake" is supported in gcc-11, otherwise use the Skylake architecture, no additional options are needed.

New Intel Xeon CPUs are likely compatible with Rocketlake & Icelake builds if not directly supported by the compiler. Although Cascadelake is an HEDT architecture it doesn't include VAES and SHA which are included on all new CPUs and used by cpuminer-opt. YMMV.

Many examples for various desktop CPU architectures can be found in file build-allarch.sh

Additional instructions for static compilation can be found here at https://lxadm.com/Static_compilation_of_cpuminer Static builds should only considered in a homogeneous HW and SW environment. Local builds will always have the best performance and compatibility.

Start mining.

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

Building for Windows

Windows compilation using Visual Studio is not supported. Mingw64 is used on a Linux system (bare metal or virtual machine) to cross-compile cpuminer-opt binaries package for Windows. That procedure follows this simpler one. Minimal knowledge of the Unix file system and bash shell are required. Some steps in common with building for Linux may not be explained in the same detail here, refer to the Linux procedure for more details.

Building for Windows, the easy way

cpuminer-opt can now be built on Windows for Windows using MinGW-w64 and MSYS2. Windows 7 or newer is required. The compiled executable can only run on the system where it was built.

Install MSYS2

Download and install the latest version of msys2-x86_64 from https://www.msys2.org/. Update packages and install mingw toolchain but leave the MSYS2 MSYS window open at the end to install more packages in the next step.

Install additional packages

Install the following additional packages required for compiling cpuminer-opt

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

Other desired packages can also be installed at this time. Some useful ones not installed by default include git, openssh, wget, vim, zip, unzip ...

Update system Path variable

Add MSYS2 to the system Path environment variable to allow cpuminer-opt to run from a Windows terminal, otherwise it can only run from an MSYS2 terminal.

https://stackoverflow.com/questions/6951938/libgmp-10-dll-is-missing

Download cpuminer-opt

Download and decompress cpuminer-opt source code. Download the tar.gz file as it preserves file permissions, specifically executability. It can be dowloaded from the Windows web browser or from MSYS2 MinGW-w64 terminal. This is different from the terminal used for package installation.

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

Cpuminer-opt can be downloaded from the MSYS2 terminal using git or wget. Git will download the most recent source code, it matches the latest release.

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

$ cd cpuminter-opt

Wget will download specific release X.Y.Z.

$ wget https://github.com/JayDDee/cpuminer-opt/releases/tag/vX.Y.Z

$ tar xvf cpuminer-opt-X.Y.Z.tar.gz

$ cd cpuminer-opt-X.Y.Z

Compile

"--param=evrp-mode=legacy" is required to workaround a bug in the compiler's optimizer. "-D_WIN32_WINNT=0x0601" is required to support Windows CPU groups. CPU groups requires Windows 7.

Running build-msys2.sh script will compile with the above options and should be used in most cases:

$ ./build-msys2.sh

Otherwise run the following commands adjusting the options as desired:

$ ./autogen.sh

$ CFLAGS="-O3 --param=evrp-mode=legacy -march=native -Wall -D_WIN32_WINNT=0x0601" ./configure --with-curl

$ make -j [N]

[N] is the number of CPU threads used by the compiler.

Run cpuminer.exe

Run cpuminer.exe directly in an MSYS2 terminal.

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

Or, if the Path variable was set above, it can run in a Windows terminal on the same system. It can also be copied or moved to a more convenient location.

> cpuminer.exe -a algo -o url -u username -p password

cpuminer.exe --help will give a list of options.

Building for Windows, the hard way

This procedure is only used to build the Windows binary release package and is done from a Linux environment. It is not intended for users.

These instructions were written specifically for Ubuntu-20.04.

It is assumed a system with Ubuntu-20.04 already available and running and the user has enough Linux knowledge to find and install packages and follow these instructions.

First it is a good idea to create new user 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 you are using /usr/lib, which isn't recommended.

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

You can speed up "make" by using all CPU cores available with "-j n" where n is the number of CPU threads you want to use.

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.

If you insist on using this procedure to build a single executable this step is required everytime you login 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 --with-crypto=$LOCAL_LIB/openssl --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.

You can compile more generically for a set of specific CPU features if you know what features you want:

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

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

If you have a CPU with more than 64 threads and Windows 7 or higher you can enable the CPU Groups feature by adding the following to CFLAGS:

-D_WIN32_WINNT=0x0601

Once you have run configure successfully run the compiler with n CPU threads:

$ 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.

Building on ARM

Linux is currently supported. MacOS & Windows can use a Linux VM, host must be a compatible ARM CPU with AArch64. Use the Linux build procedure with the following modifications:

Add -flax-vector-conversions to CFLAGS, or use ./arm-build.sh. AES and SHA2 will automatically be compiled if the CPU supports them or they can be selected manually. See armbuild-all.sh for examples.