Use the independent toolchain in Android NDK to develop C/C ++ programs

Source: Internet
Author: User

 

Some ARM toolchains can be found online, but the Android system uses Bionic libc instead of glibc. Therefore, only static compilation programs can be used.

 

In fact, Android NDK comes with toolchain, but you cannot directly use the toolchain in the ndk directory. Otherwise, the crtbegin_dynamic.o file cannot be found.

 

That is, you can use-L to specify the directory or directly put it in the gcc command line, and still prompt that the file cannot be found. (Refer to the link attached at the end ).

 

In fact, Android NDK provides a script to strip out a separate toolchain, The Script Name Is make-standalone-toolchain.sh

 

 

 

1. Download Android NDK

 

Http://developer.android.com/sdk/ndk/index.html

 

I use a android-ndk-r6b.

 

 

 

2. Extract toolchain

 

 

 

See docs/STANDALONE-TOOLCHAIN.html for Reference

 

Decompress the NDK in linux, and then extract it to/opt;

 

Android-ndk-r6b/cd/opt/

 

Build/tools/make-standalone-toolchain.sh -- platform = android-8

 

 

Expr: warning: unportable BRE: '^ \ ([^ \-]. * \) $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Expr: warning: unportable BRE: '^ \ (-- [^ =] * \) =. * $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Expr: warning: unportable BRE: '^ -- [^ =] * =\\ (. * \) $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Auto-config: -- toolchain = arm-linux-androideabi-4.4.3

Copying prebuilt binaries...

Copying sysroot headers and libraries...

Copying libstdc ++ headers and libraries...

Expr: warning: unportable BRE: '^ \ ([^ \-]. * \) $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Expr: warning: unportable BRE: '^ \ (-- [^ =] * \) =. * $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Expr: warning: unportable BRE: '^ \ (--. * \) $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Expr: warning: unportable BRE: '^ \ ([^ \-]. * \) $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Expr: warning: unportable BRE: '^ \ ([^ \-]. * \) $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Expr: warning: unportable BRE: '^ \ ([^ \-]. * \) $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Expr: warning: unportable BRE: '^ \ (-- [^ =] * \) =. * $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Expr: warning: unportable BRE: '^ -- [^ =] * =\\ (. * \) $': using '^' as the first character

Of the basic regular expression is not portable; it is being ignored

Creating package file:/tmp/ndk-hansel/arm-linux-androideabi-4.4.3.tar.bz2

Cleaning up...

Done.

 

 

 

There are some warnings that don't matter and eventually get a compressed package/tmp/ndk-hansel/arm-linux-androideabi-4.4.3.tar.bz2

 

Note: This is the directory on my Linux machine.

 

 

 

3. decompress the separate toolchain

 

You can decompress the package to any directory. Assume It is/opt/

 

 

 

4. Try writing a hello world Program.

 

Hello. c

 

 

View plain

# Include <stdlib. h>

# Include <stdio. h>

 

 

Int main (int argc, char * argv [])

{

Printf ("Hello Andriod. \ n ");

Return 0;

}

 

Makefile

 

 

 

View plain

Export PATH: =/opt/android/arm-linux-androideabi-4.4.3/bin: $ {PATH}

CROSS_COMPILE = arm-linux-androideabi-

CC = $ (CROSS_COMPILE) gcc

LD = $ (CROSS_COMPILE) ld

 

PROG = hello

OBJS = hello. o

 

$ (PROG): $ (OBJS)

$ (CC) $ (LDFLAGS)-o $ @ $ (OBJS)

 

%. O: %. c

$ (CC)-c $ (CFLAGS) $ <-o $ @

 

Clean:

Rm-rf *. o $ (PROG)

 

Compile:

Make

 

You can get the hello executable file.

 

$ File hello

Hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

 

It can be seen that the link is dynamic.

 

 

Upload the file to your mobile phone. If you have connected your mobile phone with a data cable and installed the Android SDK, you can use the adb command.

 

Adb push hello/system/sbin/hello

 

Adb shell chmod 777/system/sbin/hello

Adb shell/system/sbin/hello

If you do not have an SDK, you can install a QuickSSHd program on your mobile phone and connect to your mobile terminal using Putty or other software such as WiFi. Transfer files through SFTP.

 

 

#./Hello

Hello Andriod.

 

 

Note: The mobile phone requires the root permission.

Refer:

 

Http://groups.google.com/group/android-ndk/browse_thread/thread/7506768ccf52cea2? Pli = 1

From Hansel's column

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.