Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross compiling static C hello world for Android using arm-linux-gnueabi-gcc

I want to build a static hello world from C using arm-linux-gnueabi-gcc as opposed to using the NDK standalone toolchain or Codesourcery for that matter.

In Ubuntu...

I have done the following:

sudo apt-get install gcc-arm-linux-gnueabi

I created a hi.c like this:

#include <stdio.h>

int main(int argc, char** argv) {
   printf("hello world\n");
   return 0;
}

I have compiled it like this:

arm-linux-gnueabi-gcc -static hi.c -o hi 

I ran it on an emulator like this:

adb push hi /data/hi
adb shell /data/hi

But, I get this:

[1]   Illegal instruction     /data/hi

What step have I forgot? Based on past experience this "should" have worked, but I obviously messed this up.

like image 808
corbin Avatar asked Feb 17 '12 08:02

corbin


3 Answers

Try specifying the architecture/cpu. It sounds like the compiler is creating code with a higher architecture version than the emulator can handle.

This might work:

arm-linux-gnueabi-gcc -static -march=armv5 hi.c -o hi
like image 57
Leo Avatar answered Nov 16 '22 20:11

Leo


It worked for me with CodeBench compiler on ubuntu desktop. https://sourcery.mentor.com/sgpp/lite/arm/portal/release2029

Just create a static binary with this command:

arm-none-linux-gnueabi-gcc -o hello -static hello.c

then push it to phone

adb push hello /data/local/tmp

go run it:

adb shell
$ chmod 755 /data/local/tmp/hello
$ /data/local/tmp/hello

This will print Hello World on terminal. Same can be done from phone also. Use terminal emulator or SL4A bash shell to execute.

like image 43
Srikant Avatar answered Nov 16 '22 21:11

Srikant


If I do this on a Debian machine (VM in my case), all seems well. I am not sure what when wrong with doing similar on ubuntu. It could be as Leo suggested, but I cannot confirm. This should work for you though.

http://www.cnx-software.com/2012/01/16/installing-emdebian-arm-cross-toolchain-in-debian/

Someone added this link, but it is not using the toolchain I mentioned in the description. Leaving it in case anyone is interested.

http://tariqzubairy.wordpress.com/2012/03/09/arm-binaries-static-library-for-android/

like image 3
corbin Avatar answered Nov 16 '22 19:11

corbin