Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a C program for Genymotion (Android x86)

I'm trying to build a simple HelloWorld application and run it on Genymotion. My building system is Ubuntu 14.04 64bit. I have Android SDK(r22.6.2) and NDK(r9d) installed.

First Attempt: Build using the prebuilt NDK ARM toolchain

TOOLCHAIN = $NDK/toolchains/arm-linux-androideabi-4.8
$TOOLCHAIN/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc --sysroot=$PLATFORM hello.c -o hello
adb push hello /sdcard/
./hello

I get an error

/system/bin/sh: ./Hello: not executable: magic 7F45

After doing some research, I quickly realize Genymotion has x86 arch instead of ARM

Second Attempt: Build using x86 Standalone ToolChain

  1. Make x86 standlone toolchain first

    cd $NDK/build/tools
    ./make-standalone-toolchain.sh --arch=x86 --platform=android-19 --install-dir=/tmp/android-toolchain --ndk-dir=../../. --system=linux-x86_64
    
  2. Build Hello using the toolchain

    cd tmp/android-toolchain/bin
    ./i686-linux-android-gcc -o Hello -c hello.c
    
  3. Run Hello on Genymotion

    adb push hello /data/local/tmp
    adb shell
    cd /data/local/tmp
    ./hello
    /system/bin/sh: ./Hello: not executable: magic 7F45
    

Ran uname -a on the genymotion machine. Android System Name Ran file Hello on the executable Hello File info

So it should be a match.

Still getting the same error. Would really appreciate it if someone could shed some light.

Edit 1: Tried running the executable in /data/local/tmp instead of /sdcard/. Still gives the same error.

Edit 2: Ran uname on genymotion system and file on the executable to see if they match.

like image 743
HBZ Avatar asked Oct 23 '15 12:10

HBZ


1 Answers

If plain GCC doesn't get your job done, you'll have to take the harder route.

You need a cross-toolchain; I suggest generating a Canadian cross toolchain using Crosstool~ng; you can use NDK's standalone toolchain as a starting point.

like image 104
Shark Avatar answered Nov 19 '22 00:11

Shark