Today we are going to see how to compile a C++ application for an ARM processor from a computer with x86 or x64 architecture.
To do this, first of all, we must install the necessary dependencies.
sudo apt-get install libc6-armel-cross libc6-dev-armel-cross binutils-arm-linux-gnueabi libncurses5-dev build-essential bison flex libssl-dev bc
Next, we install the appropriate version of G++ for our ARM architecture. One of the biggest complications is the designation of ARM processors, which determines the necessary dependencies.
If in doubt, this post can be helpful. But, in summary, ARMv7 and earlier processors are 32-bit, while ARMv8 and later are 64-bit (compatible with 32-bit).
On the other hand, there are cross compilers for floating point operations executed by software (such as gnueabi) and floating point operations implemented in hardware (such as gnueabihf). If possible to use the latter, it is preferred over the former.
So, depending on the type of processor we have, we do the following,
For ARM 32bits
sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
For ARM 64bits
sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
Now, to test, let’s create a simple “hello world” program. To do this, we create a ‘main.cpp’ file with the following content.
int main(int argc, char *argv[]) {
cout << "Hello world!" << endl;
return 0;
}
To compile it, we run the following command.
For ARM 32bits
arm-linux-gnueabihf-g++ main.cpp -o hello
For ARM 64bits
aarch64-linux-gnu-gcc main.cpp -o hello
We move the ‘hello’ file to our target machine, and change the execution permissions (for example, to 777).
We run our test program with the following command.
./hello