27

Is there any way to compile GCC for android? Basically, have the GCC compiler accessible in an android terminal emulator and able to compile a binary that will run on android from a C or C++ source file.

My intention is to use this and eventually install make.

glen3b
  • 693
  • 1
  • 8
  • 22
  • You typically run a GCC *cross compiler*, build on a workstation (your PC, for example), then download to your target (the Android). It sounds like you want to actually compile and run on your Android itself. If so, the first thing you're going to need is a shell :) This link might be of use: [Building for Android](http://wiki.gnashdev.org/Building_for_Android). Also this command: `adb shell`. – paulsm4 Aug 10 '12 at 02:28
  • Basically the same question: http://stackoverflow.com/questions/6380846/gcc-on-arm-android (not very complete answer). – FooF Aug 10 '12 at 04:19
  • @paulsm4, you are correct, and I have [Kbox shell](http://kevinboone.net/kbox.html) on my device. – glen3b Aug 10 '12 at 16:11
  • related http://android.stackexchange.com/questions/13163/how-can-i-compile-native-applications-on-my-rooted-phone – Ciro Santilli OurBigBook.com Feb 24 '16 at 10:06

1 Answers1

9

The Android NDK already includes the complete GNU toolchain which runs on your computer. You should be able to use it to compile native versions of whatever program you want.

In the NDK, see docs/STANDALONE-TOOLCHAIN.html for setting up the cross-compiler. You probably want to follow the steps for "Invoking the compiler (the easy way)" to set up a copy of the toolchain which you should be able to use with configure scripts commonly included with GNU applications (like gcc).

This really is a non-trivial task and I recommend you read the docs very carefully. It also contains various values for CFLAGS, LDFLAGS etc. that you will need to customize and use when configuring and compiling your programs.

As for compiling gcc, make, etc., themselves, it's probably not necessary or desirable to do so. gcc, for instance, is going to be huge and you may not have enough storage on the phone to install it. If your ultimate intention is to compile some program to run natively on the Android device, then I would recommend you just use the NDK and point the app's configure script at it.

Michael Hampton
  • 9,737
  • 4
  • 55
  • 96
  • Thank you, I now have a working cross compiler, except no matter how I try, I cannot execute the binaries on the device. Everything except the shell says the file exists, but it complains it cannot find the file when I am in the same directory is it! But ls shows it. – glen3b Aug 11 '12 at 04:25
  • Is the directory in your PATH? And is the file marked executable? :) – Michael Hampton Aug 11 '12 at 05:23
  • I use ./{binary name} after I just ran chmod 777 {binary name} with no errors. Double-checked. – glen3b Aug 11 '12 at 15:02
  • What about this : https://android-review.googlesource.com/#/c/247498/ ? – Olórin Oct 26 '16 at 20:05
  • @MichaelHampton well, with gcc 4.9 from 2013 as the latest version, it’s not up to date whereas using `-O3` is less problematic on newer versions. This answer should be updated. At least for cross-compiling. – user2284570 Apr 05 '20 at 15:59