Wednesday, February 2, 2022

[SOLVED] ./app_name: cannot execute binary file: Exec format error

Issue

I have compiled an application in QT in my 64-bit Ubuntu machine. I need to put that application in Raspberry Pi running 32-bit Raspbian Stretch. However, when I run my app (app_name) using the command ./app_name it outputs cannot execute binary file: Exec format error. I searched across internet that it is because I compiled the program in 64-bit machine so it won't work on 32-bit. Is that the only case or I need to compile the program in the same architecture as the raspbian? Or will it work if I download a 32-bit version of Ubuntu and compiled the program there before transfering it to Raspberry Pi?


Solution

You'll need to build the program using a compiler and linker that targets the same architecture as the Raspberry Pi. That is, it should be a compiler for the specific CPU (or, at least, CPU range), that supports the same ABI (application binary interface), and the same bit-width.

In practice this means getting and configuring a Pi-compatible ARM toolchain for your Ubuntu machine, or building the program on the Pi itself. If you take the toolchain approach, you'll probably have to obtain Pi versions of any dependencies (libraries) your application uses.

Cross-compiling for the Pi is a rather specialized task, like cross-compiling for ARM Android (to some extent the same tools can be used).

In my experience, it's nearly always easier to build on the Pi. Installing the compiler and build tools is usually a matter of running "apt-get" or something similar, and any dependencies your application needs will already be of the correct specification. Modern Pi models are sufficiently fast that even substantial applications can be compiled on board. Unless you have a specific reason to cross-compile from some other system, building on board is usually easier.

If you really have to cross-compile, it might be easier to use some of the Docker images that contain complete tool-chains -- a web search should turn these up. Assembling the complete toolchain from scratch, particular if you have complex dependencies, is a non-trivial undertaking.



Answered By - Kevin Boone
Answer Checked By - Pedro (WPSolving Volunteer)