CMake and GNU Multiple Precision Arithmetic Library on ARM Cortex M4

For some recent prototyping work, I needed access to an arbitrary precision arithmetic library that would run on ARM. Specifically, I was using an STM32F4 Discovery Board (STM32F429I-DISC1) and wanted to try out the GNU MP Bignum Library on ARM Cortex M4.

The following steps and simple project I put together shows how to cross compile GMP for ARM Cortex M4, and how to incorporate it into an example program that would run on the discovery board.  I make use of CMake for most of my C/C++ projects and was happy to leverage CMake for STM32.

I assume you already have GNU ARM Embedded Toolchain installed and working, along with a copy of stm32-cmake. I also use openocd in conjunction with gdb for flashing and providing semihosting (using rdimon). It is also assumed you have STM32CubeF4 installed.

These steps were executed on a MacBook Pro but should work on Linux with minimal change.

So, the steps involved are.

  1. Cross compile GMP (on Mac) to ARM4 target.
  2. Incorporate static library and header into demo program for discovery board.

Compiling GMP for ARM M4 was done using the following steps.

mkdir tmp
cd tmp/
curl -O
tar -xf gmp-6.1.2.tar.bz2 
cd gmp-6.1.2/
mkdir build_stm32f4
cd build_stm32f4/
export PATH=/Users/frank/opt/gcc-arm-none-eabi-6-2017-q1-update/bin/:$PATH
../configure CC=arm-none-eabi-gcc CFLAGS="-nostartfiles --specs=nosys.specs -mcpu=cortex-m4" \
   --host=arm-none-eabi --disable-assembly

After configure has executed, run make. This should result in the following files under the .libs directory.

✔ ~/tmp/gmp-6.1.2/build_stm32f4 
19:24 $ tree .libs/
├── libgmp.a
├── -> ../
└── libgmp.lai

0 directories, 3 files

The file libgmp.a along with gmp.h are sufficient to add to your STM32F4 project.

For convenience, I created a git repository on Bitbucket that includes these files along with some sample code that compiles (on mac) using CMake for STM32.

You will need to set the paths to various resources in CMakeLists.txt. In particular, modify the following lines at the top of the file.

SET(STM32Cube_DIR /Users/frank/STM32Cube/Repository/STM32Cube_FW_F4_V1.16.0)
SET(TOOLCHAIN_PREFIX /Users/frank/opt/gcc-arm-none-eabi-6-2017-q1-update)
SET(CMAKE_TOOLCHAIN_FILE /Users/frank/other_repos/stm32-cmake/cmake/gcc_stm32.cmake)
SET(CMAKE_MODULE_PATH /Users/frank/other_repos/stm32-cmake/cmake)

You should see the following output on your openocd console after flashing the compiled code.

Hello GMP Test
Testing GMP library cross compiled for ARM
n = 11
19!  =  121645100408832000
42!  =  1405006117752879898543142606244511569936384000000000


Awesome!! You are now ready to begin the Great Internet Mersenne Prime Search 🙂


, , ,