directory

  1. The NDK – build and makefile

  2. Cmake and cMakeLists. TXT

  3. data

  4. harvest

AS 2.2 + uses CMake to compile the NDK by default.

  1. CMake encapsulates the edit build process in a high-level way to make it easier for callers to use, but instead of building the final SO directly, CMake produces a standard build document Makefile, which is then used in a general build manner.
  2. Some makefiles and cmakelists.txt exist for earlier projects. At least we can read makefiles.

Why do YOU need a Makefile? To answer this question, how is so generated? Complete the following steps

  1. Preprocessing: macro expand, macro replace, expand include GCC -e -o xxx.i xxx.c
  2. Precompile: GCC checks the code for compliance and compiles the code into assembly GCC -s -o xxx.s xxx.i
  3. Assembly: compile the. S assembly file into the. O binary file gcc-c-o xxx.o xxx.s
  4. Link: link other so, merge data segment GCC -o XXX xxx.o

Involves multiple file directory at compile time, to perform the operation to all documents, if change a file, the file to recompile the link associated to do, is very troublesome, and the emergence of a makefile is to deal with the pain points, through the makefile configuration and rules, realize the automation of compiling, improve efficiency. You can configure Android.mk and application. mk on AndroidStudio and compile them into dynamic or static libraries using ndk-build.

1. Makefile parsing

1.1 Makefile Rules

A complete Makefile contains five things: explicit rules, implicit rules, variable definitions, indicators, and comments.

According to the rules

The target... : prerequisites... [Tab] commandCopy the code

Target: The target of the rule, usually the filename or intermediate to be generated at the end. It could be a.o file, or it could be the final executable. It can also be “dummy target”, the name of an action that make performs, such as “clean”, which the dummy rule needs to use. PHONY statements

Prerequisites: Rule dependency. Generates a list of file names required by target.

Command: indicates the command line of a rule. Any shell or a program that can be executed under a shell. A rule can have multiple command lines, but each command occupies one line. Note: Every command line must start with the [Tab] character, which tells make that this line is a command line

By default, make executes the first rule in the Makefile, and the first target of this rule is called the “end goal.”

  1. Object file does not exist, use its description rule to create it;
  2. The object file exists, and any of the.c source files or.h files on which the object file depends are “updated” (modified since the last make) than the object file. Recompile it according to the rules;
  3. If the target. O file exists, and the target. O file is “updated” than any of its dependencies (the.c source file, the.h file) (its dependencies have not been modified since the last make), nothing is done.

The pseudo target

.PHONY:xxx
Copy the code

There are two reasons to use pseudo targets:

  1. The purpose of this target is to execute a number of commands without creating the target.
  2. Improve the efficiency of make execution

Variables and functions

"$(objects)" $(xx) : takes the value of the variableCopy the code

Automation variables for pattern rules

$^: indicates all the dependent files in the rule, which form a list separated by Spaces. If the list has duplicates, the duplicates will be eliminated. $<: indicates the first dependent file in the rule.Copy the code

Two common functions

$(wildcard PATTERN) Lists all filenames in the current directory that match the PATTERN. Wildcard: SRC = $(wildcard *.c) -- patsubst obj = $(patsubst %c,%o,$(SRC)) -- patsubst obj = $(patsubst %c,%o,$(SRC))Copy the code

Include: includes other makefiles in a Makefile

-include: ignore the error message that the include file does not exist or cannot be created

1.2 Usage of android.mk

The basic format is as follows

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) include $(LOCAL_PATH)/xxx/xxx.mk LOCAL_MODULE := xxx LOCAL_C_INCLUDES  := xxx LOCAL_SRC_FILES := xxx.c LOCAL_SHARED_LIBRARIES := xxx LOCAL_LDLIBS := xxx include $(BUILD_SHARED_LIBRARY)Copy the code

Let’s look at them one by one

LOCAL_PATH := $(call my-dir)  
Copy the code

My-dir is a macro that returns the folder path of android. mk. Call uses a makefile function, LOCAL_PATH := $(call my-dir), to assign LOCAL_PATH to the directory where the current file is located.

include $(CLEAR_VARS)
Copy the code

CLEAR_VARS is responsible for cleaning up LOCAL_xxx in addition to the LOCAL_PATH just defined above.

include $(LOCAL_PATH)/xxx/xxx.mk
Copy the code

If you have a large project, you will create a makefile in your own folder for easy development and maintenance, and include them when compiling.

Define some LOCAL_ variables with the following meanings

LOCAL_MODULE: generates the name of so, without prefix lib and suffix. LOCAL_SRC_FILES: source files to compile LOCAL_SHARED_LIBRARIES: LOCAL_LDLIBS: Linked libraries do not generate dependencies, for libraries that do not need to be recompiled include $(BUILD_SHARED_LIBRARY)Copy the code

BUILD_SHARED_LIBRARY: is a variable provided by the Build System that points to a GNU Makefile Script.

1.3 Application. Mk

The basic format is as follows

APP_PLATFORM = android-16
APP_ABI := armeabi-v7a arm64-v8a x86
APP_STL := c++_static
APP_CPPFLAGS := -exceptions -fno-rtti
APP_OPTIM := release
Copy the code

Let’s analyze it

APP_PLATFORM: Specifies the Level of Android API for which the application is built, and corresponds to the minSdkVersion of the application

When using Gradle and externalNativeBuild, this parameter should not be set directly. MinSdkVersion determines the lower limit of support.

Note: Gradle’s externalNativeBuild will ignore this APP_ABI

APP_STL: the C++ standard library for this application. By default, system STL is used. Other options include c++_shared, c++_static, and none

APP_CPPFLAGS: flags passed for all C++ compilations in a project

APP_OPTIM: a release or debug.

With the NDK-build tool and the makefile compile configuration file, why introduce the Cmake build tool? Let’s continue our study together.

2, Cmake build

What is “Cmake” : “cross Platform make” is an open source, cross-platform automated build system that controls the build process with the configuration file cMakelists.txt.

Cmake is designed to solve problems where projects are large, highly correlated, and each folder has a corresponding makefile. Cmake exists to generate makefiles, so we don’t need to write makefiles, just write simple cMakelists.txt

Common variable PROJECT_SOURCE_DIR: indicates the root directory of the project

PROJECT_BINARY_DIR: The directory where the cmake command is run, usually ${PROJECT_SOURCE_DIR}/build

PROJECT_NAME: Returns the project name defined by the project command

CMAKE_CURRENT_SOURCE_DIR: The path where the currently processed cMakelists. TXT is located

CMAKE_CURRENT_BINARY_DIR: target compilation directory

CMAKE_CURRENT_LIST_DIR: Full path to cMakelists.txt

CMAKE_CURRENT_LIST_LINE: The current line

CMAKE_MODULE_PATH: Define the path where your cmake module resides, SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake), and then you can invoke your own module using the INCLUDE command

EXECUTABLE_OUTPUT_PATH: redefines the location where the target binary executable is stored

LIBRARY_OUTPUT_PATH: redefines the destination link library file location

Commonly used instructions

  1. The assignment instructions

    set(SRC_LIST main.cpp test.cpp)

  2. Message (${PROJECT_SOURCE_DIR}) message(“build with debug mode”)

  3. Specifies the minimum version of cmake

    Cmake_minimum_required (VERSION 3.10.2)

  4. Set the project name

    project(xxx)

It introduces two variables xxx_BINARY_DIR and xxx_SOURCE_DIR, and cmake automatically defines two equivalent variables, PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR.

  1. Generating a dynamic library

    add_library(xxx SHARED xxx.cpp)

  2. Specifying the source file

    aux_source_directory(. SRC_LIST)

Search for all CPP source files in the current directory and store the list in a variable.

  1. Specifies the source file of a rule

    file(GLOB SRC_LIST “.cpp” “xxx/.cpp”)

  2. Finds the specified library file

    find_library( log-lib log )

  3. Set the libraries that target needs to link to

    target_link_libraries(native-lib ${log-lib} )

The sample is as follows

Cmake_minimum_required (VERSION 3.10.2) project(" mediaJourney ") Message (${PROJECT_SOURCE_DIR}) Message (${PROJECT_NAME}) "PROJECT_BINARY_DIR :"${PROJECT_BINARY_DIR}) message(${CMAKE_CURRENT_SOURCE_DIR} \n ${CMAKE_CURRENT_BINARY_DIR}) message("--->") message(${mediajourney_SOURCE_DIR}) add_library( native-lib SHARED native-lib.cpp ) find_library( log-lib log ) target_link_libraries( native-lib ${log-lib} )Copy the code

CXX /cmake/debug/${ABI}/build_output.txt is displayed

CMake Warning (dev) at /Users/yabin/work/av/mediaJourneyNative/app/src/main/cpp/CMakeLists.txt:7: Syntax Warning in cmake code at column 47 Argument not separated from preceding token by whitespace. This warning is for  project developers. Use -Wno-dev to suppress it. /Users/xxx/work/av/mediaJourneyNative/app/src/main/cpp mediajourneyPROJECT_BINARY_DIR :/Users/xxx/work/av/mediaJourneyNative/app/.cxx/cmake/debug/x86 /Users/xxx/work/av/mediaJourneyNative/app/src/main/cpp /Users/xxxx/work/av/mediaJourneyNative/app/.cxx/cmake/debug/x86 ---> /Users/xxx/work/av/mediaJourneyNative/app/src/main/cpp ---> Configuring doneCopy the code

Third, information

“GNU_makefile Chinese Manual” “Cmake Actual Combat” [cmakelists. TXT syntax introduction and example drill]: blog.csdn.net/afei__/arti…

Four, harvest

  1. Learned the ndK-build and cmake compilation build tool process
  2. Understand the specification and basic use of makefiles
  3. Understand the basic use of cmaklists. TXT

Thank you for reading

In the next few articles, we will review some important knowledge of C and CPP. We will master the language knowledge first and then study and explore the next step. Welcome to follow the public account “Audio and Video Development Journey” and learn and grow together.

Welcome to communicate