Remove All Ads from XDA

[GUIDE]Cross Compiling C/C++ for ARM on Linux

186 posts
Thanks Meter: 95
By bynarie, Senior Member on 11th January 2015, 05:07 AM
Post Reply Email Thread
I wrote this guide to make it easy for everyone to be able to cross compile c/c++ for android, the easy way that I know. I admit, I am a total n00b to c/c++. I started learning to cross compile when I got my new nexus 9 for christmas. I, for some reason, couldnt get busybox installed on it. So, I had to manually compile and load it on there. After reading a hundred tutorials and none of them working, I got frustrated. Finally, I compiled a working version. And it was SO EASY. Anyway, Im specifically giving instructions for Ubuntu 14.04/14.10. First thing you need to do is download a package called binutils-arm-linux-gnueabi.

- sudo apt-get install binutils-arm-linux-gnueabi
- sudo apt-get install binutils-aarch64-linux-gnu (for aarch64, or arm 64bit, or armv8)
- sudo apt-get install binutils-arm-linux-gnueabihf (for armhf)

To compile busybox for your platform, you must download the source package you want to compile from Then extract it

- tar xvf busybox.tar.gz

Then, cd into your root busybox folder.

- export ARCH=arm
- export CROSS_COMPILE=arm-linux-gnueabi- (dont forget the trailing dash(-))

This will setup your variables for compilation. Then

- make menuconfig (if you dont get a config menu, you need to "sudo apt-get install libncurses5-dev")

Now, go into the busybox settings -> Build options. Select the option "build busybox as static executable". You can select/deselect options by hitting the space bar. [ESC] key will take you back. Make sure to save your new configuration. Its almost time to compile! One last thing. If you want to add your name to it, go into the Makefile in the root busybox folder. At the top of the file, where it says EXTRAVERSION = .git, you can add something like -bynarie or whatever you want. That way when you run busybox on the terminal emulator on android, it will print out something like "BusyBox v1.24.0-bynarie". OK! Time to compile. Really simple:

- make install

This will compile every applet, and put everything in a folder called _Install. You will have busybox and busybox_unstripped in the root directory. Please, be aware that this busybox puts an applet named "su" in the bin folder in the _install folder. So, DO NOT COPY THE SU APPLET INTO YOUR ANDROID BIN FOLDER, OR YOU WILL LOSE ROOT!!! Now, to verify it compiled to the right architecture, do "file busybox" at the term and it should spit out something like "ELF 32bit ARM executable". If this is the case, congrats. We have successfully compiled busybox for android arm!! Copy the needed files over to your device and set permissions and you are done. The suggested way to move the busybox binary to your device is as follows:

- adb push busybox /data/local/tmp/busybox
- open adb shell and do "chmod 755 /data/local/tmp/busybox" or chmod it on your linux box before pushing.
- open file manager on device and move busybox to /system/xbin
- If properly done, should work.

For other nix programs like tar, the procedure is as follows:
- Open term, cd into root source folder
- ./configure CC="arm-linux-gnueabi-gcc" CPP="arm-linux-gnueabi-cpp" --host=arm-linux-gnueabi
- make

The CC variable is your C compiler command and the CPP variable is the C preprocessor, if you need to add a C++ compiler just add CXX="arm-linux-gnueabi-g++" but TAR is specifically C only. You can add all three variables I would assume to be safe. Most of the time, the readme or install documents will give you some guidance on cross compiling. But, this is how I successfully compiled TAR for arm and aarch64.

If you want to compile small/single source file c/c++, you will be using the same tools, but in a different way. Cd into your source file directory, and depending on which type of source it is (c or cpp), you will do the following:

- arm-linux-gnueabi-gcc helloWorld.c -static -o helloworld.out (for C)
- arm-linux-gnueabi-g++ helloWorld.cpp -static -o helloworld.out (C++)

Yep, its that easy. I cant guarantee 100% this will work on everything, but its a good starting point. And you dont even have to fool with the Android NDK. I find that the android ndk would mostly benefit "Apps" that need to integrate c/c++ code into them, not little console apps run from the terminal emulator. I hope someone finds this guide useful and if anyone has any problems trying to get somethin to work, you can respond and I will do my best to help.
The Following 2 Users Say Thank You to bynarie For This Useful Post: [ View ] Gift bynarie Ad-Free
19th January 2015, 12:11 AM |#2  
USBhost's Avatar
Recognized Contributor
Flag u:r:usbhost:s0
Thanks Meter: 6,780
Donate to Me
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes