Compiling Android c/c ++ program for ndk development, ndk android
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.