Choosing the Right CPU Flags for GCC on Raspbian

Choosing the Right CPU Flags for GCC on Raspbian

I’ve been messing with the CPU flags for GCC on Raspberry PI. Raspberry PI1 uses a 32 bit ARM6 cpu, the Broadcom BCM2835. The PI2 uses a 32 bit ARM7 cpu, the Broadcom BCM2836. The PI3 uses a 64 bit ARM8 cpu, the Broadcom BCM2837.

In order to make all Raspberry PIs binary compatible, Raspbian is configured to use only 32 bit opcodes. I’m not sure how it works on the PI1. But for Pi2 and Pi3, Raspbian is configured to build for ARM7 architecture.

But you can easily build your programs with the ARM8 opcodes on PI3, if you don’t care about binary compatibility with PI2. When building set the following flags for PI3:

-mcpu=cortex-a53 -mfpu=neon-fp-armv8

To build in 32 bit mode, compatible with PI2 use:

-mcpu=cortex-a7 -mfpu=neon-vfpv4

Programs built with the arm8 opcodes seem to run fine on 32-bit Raspbian. I have not profiled if they are faster.

But there is on problem: ARM8 built programs are not easily debuggable on stock Raspbian. First tools like valgrind will crash. The one that comes in apt-get for PI is ARM7, and will crash when it hits any ARM8 opcodes – or at least mine seems to. Gdb has trouble printing stack traces.

Its probably not worth building ARM8 binaries on ARM7 Raspbian.