Skip to content

Build C/C++ Executables for android using NDK

March 24, 2013

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 name
LOCAL_MODULE    := hello_world  
# list your C files to compile
LOCAL_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.

Advertisements

From → Android, Linux, ndk

27 Comments
  1. It’s appropriate time to make some plans for the future and it’s time to be happy.
    I have learn this publish and if I could I desire to counsel you
    few fascinating issues or suggestions. Maybe you could write subsequent articles referring
    to this article. I desire to read even more things about it!

  2. This was insanely helpful.
    Tried using a ‘agcc.pl’ script to no avail, this worked perfectly.

  3. Glad, it helped.

  4. My website site http://learnfromthegroundup.com is similar, but give a way to compile c++ executables with ndk. Terminalide on android can now do c executables . When you run install_gcc to install the c compiler. The c compiler is name terminal-gcc. You can also program in assembly using syscalls or the c standard library.

  5. I am more involved in development of android based embedded systems rather than mobile phones, so I was not aware of this termialide, looks an amazing tool on an android mobile. I will surly try that out.

  6. Kelz permalink

    I have an existing linux C++ lib that i need to port over to android. The problem that i am having is with the makefile to android mk file. any tips?

    thanks

  7. You may have two options for this,
    1. Convert your Makefile to Android.mk. You can use this Android makefile explanation.
    http://www.netmite.com/android/mydroid/1.6/development/ndk/docs/ANDROID-MK.TXT
    If you are not using any library “out of NDK” then It can easily gets converted in shared library.
    if NDK libs are not, It might require for you to download AOSP, in order to get all the library access.
    It will be easy if you know about Makefile.

    2. You can cross-compile your c++ lib project using any cross toolchain for example, for ARM platform you can use arm-none-linux-gnueabi-g++. There are different tool-chain for each ARM platform. Make sure you use the right tool-chain.
    Then use that shared library as a prebuilt library in your Android project.

    I hope this helps. If you can provide me some more information like which library or for which device you are trying to port etc then I can help a little more.

    Cheers, Pratik

  8. Kelz permalink

    it should work it works when i click on it :), might be a browser issue

  9. Hey, andorid ndk now supports clang that means it has llvm but i think it’s nt directly accessible…
    Can I use llvm with ndk ? I need to create a custom compiler ( will be using llvm for that ) and then provide it as an app to users to compile n run code in app

  10. Syed permalink

    Hi, This post was really helpful to me. Can you please tell me is there a way to profile this generated executable? Waiting for your answer. Thanks in advance.

  11. Teo permalink

    Really thank you

  12. Daniel permalink

    Sorry you are not correct about not needing to compile binaries static.

    Dynamic linking may work 90% of the time or so, but every once in awhile there will be an android system that will not like it for one reason or another.

    I had this happen when compiling a sqlite3 binary.

    And when you have to compile everything static, you are more limited.

    For example, liblog.so is only available as a shared library when you use dynamic linking, not static.

    • You will need AOSP to get liblog static library. Also sqlite3 tool comes compilation comes with AOSP at location externel/sqlite/.

      Also, if you have AOSP access you can compile unavailable static libraries you need by modifying default Android.mk

      But yes, using NDK it might not be possible all the time.

  13. Worked perfectly. Well written. Thank you.

  14. Kaushal permalink

    I have downloaded android c compiler in my mobile but it requires command for starting so please you send me the details of the command or command line on my email address or email id

  15. Thanks a lot Pratik. This is great.

  16. Android NDK: Could not find application project directory !
    Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.

    how to solve it. please let me know. it will save me..
    thanks in advance.

  17. Tarek Abulnaga permalink

    This is a great post, I have just one comment, when I transfer the generated binary to my device, I can’t execute it even using adb shell, always says permission denied, should i have rooted to device to be able to execute it?

  18. English version of this comment would have been nice!..😄

Trackbacks & Pingbacks

  1. Android Root 原理 | slinuxer
  2. Cross-compile source for Android – CK.D.Lee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: