Raspberry Pi: cross-compile bcm2835 library

Environment
  • Ubuntu 16.04 LTS
  • arm-linux-gnueabihf (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
  • Raspberry Pi 3 Model B

The bcm2835 library “provides access to GPIO and other IO functions on the Broadcom BCM 2835 chip, as used in the RaspberryPi, allowing access to the GPIO pins on the 26 pin IDE plug on the RPi board so you can control and interface with various external devices”.

Install ARM toolchhain

See Raspberry Pi: C++ cross-compiling.

Download and cross-compile bcm2835
Static library

Note, that:

  • In the example below version 1.52 of the library is used – replace with the latest version.
  • The toolchain prefix arm-linux-gnueabihf- should be replaced by your toolchain prefix. See more on toolchains and cross-compiling in Raspberry Pi: C++ cross-compiling blog.
  • The library and its header will be installed in /usr/local/lib and /usr/local/include.
$ wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.52.tar.gz
$ tar zxvf bcm2835-1.52.tar.gz
$ cd bcm2835-1.52
$ ./configure -host=arm -prefix=/usr/local CC=arm-linux-gnueabihf-gcc ar=arm-linux-gnueabihf-ar
$ make
# Skip the check (test) - it will fail in Ubuntu
# sudo make check
$ sudo make install
...
 /bin/mkdir -p '/usr/local/lib'
 /usr/bin/install -c -m 644  libbcm2835.a '/usr/local/lib'
 ( cd '/usr/local/lib' && ranlib libbcm2835.a )
 /bin/mkdir -p '/usr/local/include'
 /usr/bin/install -c -m 644 bcm2835.h '/usr/local/include'
...

To change installation directories to, e.g. /usr/local/include/rpi and /usr/local/lib/rpi, use the -includedir and -libdir option instead of -prefix:

$ ./configure -host=arm CC=arm-linux-gnueabihf-gcc ar=arm-linux-gnueabihf-ar -includedir=/usr/local/include/rpi -libdir=/usr/local/lib/rpi
...
$ sudo make install
...
 /bin/mkdir -p '/usr/local/lib'
 /usr/bin/install -c -m 644  libbcm2835.a '/usr/local/lib/rpi'
 ( cd '/usr/local/lib' && ranlib libbcm2835.a )
 /bin/mkdir -p '/usr/local/include'
 /usr/bin/install -c -m 644 bcm2835.h '/usr/local/include/rpi'
...
Shared library

Quick solution without involving autotools:

$ cd bcm2835-1.52/src
$ arm-linux-gnueabihf-gcc -shared -fPIC bcm2835.c -o libbcm2835.so

Copy the shared library to the /lib on your Raspberry Pi.

Leave a Reply

Your email address will not be published. Required fields are marked *