Hello everyone,
I was wandering if I can compile C binary that runs on android platform, So a long time ago I found a method (without using ndk), with
arm-none-linux-gnueabi-gcc -static test.c -o test
Using above command, we can compile a C file named test.c as a static binary. Yes, with this method we can only compile static binaries. But I thought hey.. that is not the most convenient way to compile means I don’t want to compile my binary every time static.
So, I found an alternative (and proper) way to build C/C++ executable using Android NDK. Let’s take an example to see how it works.
Prerequisites:
1. Linux Host (not yet tried on windows)
2. Android NDK on host machine
Let’s create a simple hello world C program in file test.c
#include <stdio.h>#include <stdlib.h>int main(){printf("Hello World\n");return 0;}
So, above program will just print “Hello World” on standard output.
Now, to compile it with NDK, we need a Android.mk. the Android.mk will look like this.
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)# give module nameLOCAL_MODULE := hello_world# list your C files to compileLOCAL_SRC_FILES := test.c# this option will build executables instead of building library for android application.include $(BUILD_EXECUTABLE)
The NDK system is only able to compile in android project hierarchy.
So, we need to create a directory structure like this
hello_world|`-- jni`-- libs
Now, place test.c and Android.mk file in jni directory.
Follow these steps on command line:
$ mkdir ~/hello_world/$ mkdir ~/hello_world/jni$ mkdir ~/hello_world/libs$ cd ~/hello_world/jni/$ gedit test.c# create your C code in file, save and exit.$ gedit Android.mk# write Android.mk contents, save and exit.$ export PATH=$PATH:<path/to/ndk>/$ ndk-build
By executing “ndk-build”, ndk will compile our test.c (not static) and puts the binary in hello_world/libs/armeabi/
Advantages of using NDK:
1. We don’t have to compile binaries static, so size of binary will be reduced.
2. We can use android C/C++ libraries like liblog to print output in logcat from C.
Let me know if you have any doubts on this in comments.
Hello guys,
Linux kernel is monolithic kernel. hmmm.. but wait I don’t know what a kernel is. Well let’s have a ride in kernel coaster.
What is Kernel?
In simple words, a kernel is an interface between hardware and OS. It takes commands from OS and make hardware act upon and takes data from hardware and gives it to OS. It also provides inter process communication.
Types of Kernel
There are majorly three types of kernel
- microkernel
- monolithic kernel
- hybrid kernel
each kernel type has its advantage and disadvantage. Let us talk briefly about each type.
Microkernel
Microkernel is kernel which only does main handling i.e. CPU, memory and IPC. Everything else is managed in user mode. Now there are two modes in a system. 1) user mode and 2) supervisor mode. Everything running in OS is in user mode and everything running from kernel is in supervisor mode. As the name says, In supervisor mode process has more permissions to do stuff than user mode.
So, In microkernel only few things are running in supervisor mode and rest things have to run in user mode.Now, This has few advantages and few disadvantage.
Advantages:
With this, kernel has small install footprint as well as memory footprint. Another advantage is kernel will be more portable because it is doing main handling it can run on almost every where. the rest things can be different which are running in user mode. By this we get more security because less processes are running in supervisor mode so all rest processes does not have full rights to do things.
Disadvantages:
Now, In monolithic kernel, most of the process will run in user mode, so each process has to wait in queue to get information and also it can not do inter process communication without waiting. Hardware may run slower because drivers are running in user mode.
Monolithic Kernel:
Monolithic kernels are opposite of microkernels. They include basic handling of CPU, memory and IPC as well as it also includes device drivers, file system management, system server calls. Monolithic kernel tends to be better and multitasking because all process are running in the same mode. So waiting time of ipc and to get information can be reduced. Now this however can cause problem because many processes are running in supervisor mode and defect in one process can make whole system down. Monolithic kernel has following advantages and disadvantages:
Advantages:
In monolithic kernel, processes has more direct access to hardware. Processes has easiness to inter communication. Processes reacts faster because they don’t have to wait in queues.
Disadvantages:
Since monolithic kernel includes more things, its install/memory footprint is increased. It is less secure because many processes runs in supervisor mode.
Hybrid Kernels:
Hybrid Kernels are combination of microkernel and monolithic kernel. In hybrid kernel user can choose what needs to be run in user mode and what needs to be run in supervisor mode. So it has advantages of both types of kernel as well as disadvantages of both kernels. Windows and OS X kernels are Hybrid kernels.
Linux Kernel
Ffffff…Now we know what is a kernel, so let’s little bit about Linux kernel.
You can find Linux kernel in your /boot directory in Ubuntu. There may be different names for kernel. each type of name denotes some special about that kernel and those are,
vmlinux – normal kernel image, In early days it was unix. after that it is changed to linux and then virtual memory concept came in picture so it is called as vmlinux.
vmlinuz – zlib compressed kernel image.
zImage – compressed + virtual memory support kernel image
bzImage – zImage compressed to the max
uImage – kernel image for U-boot i.e. very popular boot loader for ARM based platform.
There are different kernel version. Version number rules are:
Linux kernel has one more important thing, that is its capability to insert / remove modules. I will talk about Linux kernel modules in later posts may be.
I guess, That is enough knowledge to start exploring in Linux kernel.
Enjoy.
$tar -xvf linux-3.6.7.tar.bz2 $cd linux-3.6.7
Now you have to configure the kernel as per your system. (So kernel can actually work on it…). If you don’t know how to configure (like me
) then you can find the existing configuration from your current system.
$cat /boot/config-2.6.32-21-generic > .config
$make oldconfig
$make -j2 && sudo make modules_install
$sudo cp arch/i386/boot/bzImage /boot/vmlinuz-3.6.7
$sudo update-grub2
menuentry 'Ubuntu, with Linux 3.6.7' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 42d5cc2e-4a0c-4312-91c2-e9ef9d0aecb7
linux /boot/vmlinuz-3.6.7 root=/dev/sda1 ro quiet splash
}
menuentry 'Ubuntu, with Linux 3.6.7 (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
insmod ext2
set root='(hd0,1)'
search --no-floppy --fs-uuid --set 42d5cc2e-4a0c-4312-91c2-e9ef9d0aecb7
echo 'Loading Linux 3.6.7 ...'
linux /boot/vmlinuz-3.6.7 root=/dev/sda1 ro single
echo 'Loading initial ramdisk ...'
}
Hi all,
Let us install sun-java6-jdk on ubuntu.
On Ubuntu 10.04:
Now This are the standard method to install it.
Open your terminal by Ctrl+Alt+T
$sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner" $sudo apt-get update $sudo apt-get install sun-java6-jdk
Now I guess You will encounter with this problem in last step.
Package sun-java6-jdk is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source E: Package sun-java6-jdk has no installation candidate
Now the solution to this is:
$sudo add-apt-repository ppa:ferramroberto/java $sudo apt-get update $sudo apt-get install sun-java6-jdk
And finally you have installed sun java6 on your ubuntu 10.04
On Ubuntu 12.04:
To install sun java6 on ubuntu 12.04 follow this steps:
Open your terminal by Ctrl+Alt+T
Enter following commands and you will get sun java6 installed on your system.
$wget https://github.com/flexiondotorg/oab-java6/raw/0.2.6/oab-java.sh -O oab-java.sh
This will download the script to install sun java.
After that provide executable permission to that script.
$chmod +x oab-java.sh
And after that execute the script
$sudo ./oab-java.sh
I don’t know who is the author of this script, but hats off to that guy, that made our work so easy to install java.
Well, Now a little bit of patience, so that this script downloads some packages required.
After that execute installation commands:
$sudo apt-get install sun-java6-jre sun-java6-jdk sun-java6-plugin sun-java6-fonts
Enjoy…
#tftp -g -r <filename> <ip-addr>
#tftp -p -r <filename> <ip-addr>
$cd /tftpboot
$touch a.out
$chmod 777 a.out
#tftp -p -r a.out <ip-addr>
While working with any embedded development kit, Using TFTP to transfer files between Host PC and target device is a good solution.
Read more about tftp on this link
Basically, In this method we will create a TFTP server on Host PC which will be a kind of the directory in Host PC. The files in that directory will be available to all who supports TFTP and can do file transfers also.
One of the key usage of this TFTP server is while developing kernel images we should not flash the kernel images all the time on embedded dev kit’s flash memory. because it will reduce the flash memory’s life. for more information you can refer to this wiki link.
So, that was some briefing about TFTP and now let us see the steps to set up that server.
I am using Ubuntu 12.04.
First Install following packages:
$sudo apt-get install xinetd tftpd tftp
$gedit /etc/xinetd.d/tftp
Write following in the file tftp
service tftp
{
protocol = udp
port = 69
socket_type = dgram
wait = yes
user = nobody
server = /usr/sbin/in.tftpd
server_args = /tftpboot -s
disable = no
}
Now let us create tftpboot directory where all the files of tftp server will be stored
$sudo mkdir /tftpboot
$sudo chmod -R 777 /tftpboot
$sudo chown -R nobody /tftpboot
Now, Restart the xinet tftpd so that our tftp server gets ready to work.
$sudo /etc/init.d/xinetd stop
$sudo /etc/init.d/xinetd start
And That’s it. TFTP server is ready now.
Enjoy.
Thanks.




