Native programming on Android

Source: Internet
Author: User
Besides programming with Java, We're also able to do native programming with C or C ++ on Android platform.
To do so, we need a cross compiler target on ARM processor for our working platform (Windows or Linux ). so I downloaded sourcery g ++ lite 2009q3-68 for ARM Eabi, the tool chain for arm. if you're careful enough, You wowould have noticed my mistake which took me a day to figure out. unfortunately, I downloaded the wrong tool chain. the correct one is sourcery g ++ lite 2009q3-67 for arm gnu/Linux. this toolchain is used for target platform that runs Linux operating system. if I were patient enough to read the Getting Started Guide for the toolchain, I wocould found out that the first toolchain is for platforms that don't have operating system, and the second one is for Linux.
From the embarrassing story, you may tell I'm still a freshman on embedded system. surely I am. in the last embedded project I worked on, there is already a toolchain and building system up and running built by my experienced colleagues. so I don't need to care about this. anyway, this frustrating but finally success experience gains me a lot.

Ensuing content is about the steps to build and run Hello world with the toolchain.

  1. DownloadRightToolchain and extract the package
  2. Add bin to PATH environment variable for easier access to the toolchain
  3. Add below source code
  4.         #include    "stdio.h"

    int main ( int argc, char *argv[] )
    {
    printf("hello world!");
    return 0;
    } // ---------- end of function main ----------

  5. Compile the Code with arm-None-Linux-gnueabi-gcc h. c-static. -Static argument is necessary here because Android uses a different C library with this toolchain. we must link the application statically against C Lib, otherwise, the application will fail to run on Android emulator.
  6. Download the application to emulator with: ADB push Hello/data/
  7. Log onto emulator with: ADB Shell
  8. Add execution permission to the application with: chmod 777/data/Hello
  9. Run the application/data/hello and we get desired output

Note, because we statically linked the C library so we used the C lib provided in the codesourcery arm toolchain rather than the custom bionic C lib comes with ndk.

Update: Native programming with Android building system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.