Linux tip 'n trick

Just another way to remember!

Subscribe to RSS feed

Posts tagged with "c"

Build and run C application on Android

, , , ...

Download NDK, unzip to ~/android-ndk-1.6_r1
To compile a C source file to be run on Android platform, the cross compiler requires several parameters. Below I used "alias" to shorten the command.

$ ANDROID_NDK=~/android-ndk-1.6_r1
$ export PATH=$PATH:${ANDROID_NDK}/build/prebuilt/linux-x86/arm-eabi-4.2.1/bin
$ alias agcc='arm-eabi-gcc --sysroot ${ANDROID_NDK}/build/platforms/android-4/arch-arm -fPIC -mandroid -DANDROID -DOS_ANDROID'



Write a C program, suppose it is hello.c

#include <stdio.h>

int main() {
printf("Hello world!\n");
return 0;
}


Compile it by this command:

$ agcc hello.c -o hello


Push the application to the phone using ADB

$ adb push hello /sdcard


For some security reasons, you cannot change the permission to let "hello" run, we need to remount the sdcard as follow

$ adb shell


# mount
rootfs / rootfs ro 0 0
tmpfs /dev tmpfs rw,mode=755 0 0
devpts /dev/pts devpts rw,mode=600 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
tmpfs /sqlite_stmt_journals tmpfs rw,size=4096k 0 0
none /dev/cpuctl cgroup rw,cpu 0 0
/dev/block/mtdblock0 /system yaffs2 ro 0 0
/dev/block/mtdblock1 /data yaffs2 rw,nosuid,nodev 0 0
/dev/block/mtdblock2 /cache yaffs2 rw,nosuid,nodev 0 0
/dev/block//vold/179:0 /sdcard vfat rw,dirsync,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0


See the red highlighted color line? This is the sdcard! Remount it by this command

# mount -o remount,rw -t vfat /dev/block//vold/179:0 /sdcard


Then run the program

# /sdcard/hello
Hello world!