The previous 2 articles describe the command-line parameters for the Android NDK compilation, and how to use ANDROID.MK in any directory to compile local C + + code. Andriod.mk and Ndk-build are just Android officially provided a set of packaged Android cross-compilation environment, in fact, you can not use it, and directly through the traditional makefile file to compile your C/s code, This article describes how to compile a library file that can be used with the Android platform directly from a traditional makefile file .
Often engage in embedded development of friends should not be unfamiliar with the cross-compilation environment, in plain, is a set of running on the x86 PC compiler tool, you can be on the PC to compile the target platform (such as arm) recognizable binary files. The Android platform also provides such a cross-compilation tool chain, which is placed in the Toolchains directory of the Android NDK development package , so in our makefile file, just give the appropriate compilation tools.
That's the first point of the story, directly on the example, our goal is to compile the following math.c file into a static library file :
#include <stdio.h>int Add (int a, int b) {return a+b;}
You need to write a makefile file here, assuming that your Android NDK is installed in the /opt/android/ndk directory, and of course you can modify the definition of the relevant path in makefile according to your actual situation. Examples of makefile files are as follows:
# makefile written by ticktick# show how to cross-compile c/ C++ code for android platform. phony: cleanndkroot=/opt/android/ndkplatform=$ (ndkroot)/platforms/android-14/arch-armcross_compile=$ ( Ndkroot)/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-cc=$ (CROSS_COMPILE) gccar=$ (Cross_compile) arld=$ (cross_compile) ldcflags = -i$ (PWD) -i$ (PLATFORM)/usr/include - Wall -o2 -fpic -dandroid -dhave_pthread -mfpu=neon -mfloat-abi=softfpldflags =target = libmath.asrcs = $ (WILDCARD&NBSP;*.C) objs = $ (SRCS:.C=.O) All: $ (OBJS) $ (AR) -rc $ (TARGET) $ (OBJS) Clean: rm -f *.o *.a *.so
This is not about the fundamentals of the makefile file, but the considerations for writing makefile files for the Android environment.
(1) Cross_compile
The path to the android NDK compiler tool chain must be given correctly, and when the make command is executed in the directory, the compilation system will look for the corresponding compilation command based on the cross_compile prefix.
(2)-i$ (PLATFORM)/usr/include
Because the Android platform does not use the traditional C language library libc, but instead of writing a more efficient and suitable for the embedded platform of the C language library, so the system header file directory can no longer use the default path, must be until the Android platform header file directory
(3)-WALL-O2-FPIC-DANDROID-DHAVE_PTHREAD-MFPU=NEON-MFLOAT-ABI=SOFTFP
These parameters of the meaning of the online basically have introduced, I do not explain, not all must be added, but more commonly used.
Compile method:
After you have written the makefile file and saved it, you can execute the make command directly in the current directory, and when the compilation is complete, the current directory will generate LIBMATH.A and you can get directly to the JNI project of Android and use it.
For more information on how to cross-compile the Android platform using makefile files, C + + code is introduced here, have any questions welcome message or letter [email protected] exchange.
This article is from the "Shadow Three People" blog, be sure to keep this source http://ticktick.blog.51cto.com/823160/1432140