(NDK compilation) Detailed explanation of the C/C++ program process compiled using Android.mk

If you want to run C/C++ program executable files on Android devices , one method you can use is to use NDK compilation , which is often more suitable than gcc compilation . Here I use the imx6q development board loaded with Android6.0.1 written by myself. Mirror, compiled using NDK on Ubuntu 64-bit system.

Table of contents

1. Prepare documents

2.Write Android.mk

Notice

Complete Android.mk code

3.NDK compilation

4. Run the executable file


1. Prepare documents

First, in a newly created folder, place two files written by yourself, one is a C language program and the other is Android.mk. The content of the C language program can be decided by yourself, as long as it meets the standards. It can also be the simplest way to print hello, world

2.Write Android.mk

For writing Android.mk files, you need to understand some of the necessary settings first. Here I will release my Android.mk first. 

The content of the file is very short, let’s explain it in detail,

LOCAL_PATH := $(call my-dir)

First, the LOCAL_PATH variable must be defined to find source files in the development tree. The macro function my-dir is provided by the compilation system and is used to return the current path (that is, the directory containing the Android.mk file).

include $(CLEAR_VARS)

This is a necessary statement. CLEAR_VARS is provided by the compilation system and is used to clear many LOCAL_XXX variables, such as: LOCAL_MODULE, LOCAL_SRC_FILES, LOCAL_STATIC_LIBRARIES...

LOCAL_MODULE := test_app

In the compiled target object, the LOCAL_MODULE variable must be defined to identify each module described in the Android.mk file. The name must be unique and not contain any spaces. The compilation system will automatically generate appropriate prefixes and suffixes. A shared library module named 'sum' will generate the 'libsum.so' file.

LOCAL_SRC_FILES := test_app.c

The LOCAL_SRC_FILES variable must contain the C or C++ source code files that will be compiled and packaged into the module. We don't need to list the header files and include files here, because the compilation system will automatically find the dependent files for you, just list the source code files passed directly to the compiler.

include $(BUILD_SHARED_LIBRARY)

BUILD_SHARED_LIBRARY means compiling and generating a shared library. It is a variable provided by the compilation system and points to a GNU Makefile script. It is responsible for collecting all the information defined in the LOCAL_XXX variables since the last call to 'include $(CLEAR_VARS)', and deciding what to compile and how. do it right

Notice

You can see that there are two lines in my Android.mk file

LOCAL_CFLAGS += -pie -fPIE 
LOCAL_LDFLAGS += -pie -fPIE

When I did not add these two lines of code at first, I could use NDK to successfully compile the executable file, but when placed on the development board of the Android system, it could not run and an error was reported: only position independent executables (PIE) are supported

I learned about this problem and the solution by referring to error: only position independent executables (PIE) are supported. Basically, the PIE security mechanism  was introduced from Android 4.1. The solution is to add the above two statements in the Android.mk file. Just hit it

Complete Android.mk code

Given the complete Android.mk code, since my C file is called test_app.c , if you want to use it directly, you need to replace test_app in the following code with the name of the C program file on your computer.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)


LOCAL_SRC_FILES := test_app.c
LOCAL_MODULE := test_app

LOCAL_CFLAGS += -pie -fPIE 
LOCAL_LDFLAGS += -pie -fPIE

include $(BUILD_EXECUTABLE)

#APP_ALLOW_MISSING_DEPS=true

#APP_PLATFORM        := android-14
APP_ABI             := armeabi-v7a x86 x86_64 arm64-v8a

3.NDK compilation

After completing the above steps, open the terminal in the current folder and enter the ndk-build command to compile.

/home/yinlong/Music/sdkapp/android-ndk-r14b-linux-x86_64/android-ndk-r14b/ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=Android.mk

After successful compilation, two new folders appear in the current directory, and the generated executable programs are placed in the libs directory.

 You can see multiple versions in the libs directory. The one suitable for my imx6q development board isthe program under armeabi-v7a.

After opening, you can see the compiled test_app

4. Run the executable file

Put the above file into the Android device, that is, put it into my development board and run the following command to set the read and write permissions of the file. Note that you must have root permissions .

chmod 777 test_app

Then you can run the executable file compiled by NDK 

./test_app

Guess you like

Origin blog.csdn.net/danielxinhj/article/details/133073755