Compiling Android c/c ++ program for ndk development, ndk android

Source: Internet
Author: User

Compiling Android c/c ++ program for ndk development, ndk android

  • Install ndk

Download ndk from the official Android website and double-click it to decompress it to the current folder. We recommend that you install it in that folder and decompress it to that folder without spaces in the folder path, because the two strings before and after spaces are treated as two folders during gcc compilation.

  • Manually compile with gcc

To compile a program using gcc, you must first compile the makefile file and then compile it using the gcc make tool. The content of the makefile file is as follows:

 1 NDK_ROOT=C:/android-ndk-r10d 2 TOOLCHAINS_ROOT=$(NDK_ROOT)/toolchains/arm-linux-androideabi-4.9/prebuilt/windows 3 TOOLCHAINS_PREFIX=$(TOOLCHAINS_ROOT)/bin/arm-linux-androideabi 4 TOOLCHAINS_INCLUDE=$(TOOLCHAINS_ROOT)/lib/gcc/arm-linux-androideabi/4.9/include-fixed 5 PLATFORM_ROOT=$(NDK_ROOT)/platforms/android-21/arch-arm 6 PLATFORM_INCLUDE=$(PLATFORM_ROOT)/usr/include 7 PLATFORM_LIB=$(PLATFORM_ROOT)/usr/lib 8 MODULE_NAME=hello 9 RM=del10 FLAGS=-I$(TOOLCHAINS_INCLUDE)\11     -I$(PLATFORM_INCLUDE)\12     -L$(PLATFORM_LIB)\13     -nostdlib\14     -lgcc\15     -Bdynamic\16     -lc17 OBJS=$(MODULE_NAME).o\18     $(PLATFORM_LIB)/crtbegin_dynamic.o\19     $(PLATFORM_LIB)/crtend_android.o20 21 all:22     $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -c $(MODULE_NAME).c -o $(MODULE_NAME).o23     $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) $(OBJS) -o $(MODULE_NAME)24 clean:25     $(RM) *.o26 install:27     adb push $(MODULE_NAME) /data/local/tmp28     adb shell chmod 755 /data/local/tmp/$(MODULE_NAME)

The gcc make tool is located in .. \ android-ndk-r10d \ prebuilt \ windows \ bin, this directory needs to be added to the system or temporary PATH environment variable, and then hello. c. Put the makefile file in a directory, enable the virtual machine, and then execute the following commands in sequence:

makemake install adb shell /data/local/tmp/hello

You will see the familiar "hello world !!!" Output.

Remember to distinguish I and L in makefile. I didn't notice that errors are always reported during compilation.

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.