Build a Makefile

Import static and dynamic libraries

1. Create a directory under main for dynamic and static libraries, as well as.mk files

2. Add the path for storing the. Mk file in build

3. Android. Mk writing

  1. LOCAL_PATH := $(call my-dir) := $(call my-dir) := $(call my-dir)

  2. Include $(CLEAR_VARS) : [Cleanup, similar to a flag that many script instructions start with]

  3. LOCAL_MODULE := getndk:[precompiled library name, lib and suffix removed]

  4. LOCAL_SRC_FILES := libgetndk.so:[specify static library or dynamic library]

  5. Include $(PREBUILT_SHARED_LIBRARY) : [precompile static or dynamic library scripts]

  6. Include $(CLEAR_VARS) : [new tag]

  7. LOCAL_MODULE := MyLoginJar: [composited new library name]

  8. LOCAL_SRC_FILES := login. c:[C and C ++ source files needed to build the new library, each file needs to be separated by \]

  9. LOCAL_SHARED_LIBRARIES := getnDK :[link in the previous dynamic or static libraries]

  10. LOCAL_LDLIBS := -lm-llog: [log library connected]

  11. Include $(BUILD_SHARED_LIBRARY) : include $(BUILD_SHARED_LIBRARY) : [last generated dynamic or static library, generated path apk/lib/armeabi-v7a/ libmyloginjar.so]

C Test. C # 1 The location of the source file. The my-dir macro returns the path to the current directory (including the directory of the Android.mk file itself). LOCAL_PATH:= $(call my-dir) $(info "LOCAL_PATH:======== ${LOCAL_PATH}") # 2 Include $(CLEAR_VARS) # TODO precompiled library introduction == precompiled library LOCAL_MODULE := getndk # Specify static library # LOCAL_SRC_FILES := libgetndK. a # LOCAL_SRC_FILES := libgetndK. so # precompile static library Makeifle script # include $(PREBUILT_STATIC_LIBRARY) # precompile shared library Makeifle script Include $(PREBUILT_SHARED_LIBRARY) # Include other makefiles. The CLEAR_VARS variable points to a special GNU Makefile that clears many LOCAL_XXX variables for you. # Not the LOCAL_PATH variable include $(CLEAR_VARS) # TODO end # 3. Apk /lib/ libmyLoginjar. so == total library # Store the name of the module you want to build each module name must be unique and not contain any Spaces # If the module name already starts with lib, the build system does not append the extra prefix lib; Instead, take the module name as is and add the.so extension. LOCAL_MODULE := MyLoginJar # Contains a list of C and/or C++ source files to build into the module separated by Spaces LOCAL_SRC_FILES := login.c \ test. C # TODO starts linking in # static library links LOCAL_STATIC_LIBRARIES := getnDK # import log #LOCAL_LDLIBS := -llog LOCAL_LDLIBS  := -lm -llog # 4. Apk /lib/armeabi-v7a/ libmyloginjar. so include $(BUILD_SHARED_LIBRARY) # BUILD_SHARED_LIBRARY # include $(BUILD_STATIC_LIBRARYCopy the code

Second, CmakeList details

Note the lowest supported version: Cmake_minimum_required (VERSION 3.10.2) # cmake_minimum_required(VERSION 3.10.2) # Project ("ndk28_cmake") # file you can define a variable SOURCE, GLOB (use GLOB to collect a list of SOURCE files from the SOURCE tree, You can happy *. CPP *. *. C h) # https://www.imooc.com/wenda/detail/576408 file (GLOB SOURCE *. CPP *. C) # # CMAKE_SOURCE_DIR import library file CMK path #CMAKE_ANDROID_ARCH_ABI gets the architecture of the current platform. Set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -l ${CMAKE_SOURCE_DIR}/${CMAKE_ANDROID_ARCH_ABI}") # STATIC) add_library(native-lib # library name --> libnative-lib.so SHARED # CPP source file: ${SOURCE}) # Find a dynamic library in an NDK tool (liblog.so) # Think: How do I know which libraries are writable, how do you know that a log is ok? D: Android\Sdk\ NDK \21.4.7075529\build\cmake\system_libs.cmake D: \ Android \ Sdk \ the NDK \ 21.0.6113669 \ toolchains \ LLVM \ prebuilt \ Windows - x86_64 \ sysroot \ usr \ lib \ arm - Linux - 16 androideabi \ \ liblog. How do you know it is on 21.4.7075529? Arm - Linux - androideabi? , 16? # a:? 1(because local.properties is aware of the NDK version, or your current NDK version) 2(because my phone is arm32 so == arm-linux-androideabi and I have run it) # answer:? MinSdkVersion 16: find_library(log-lib log) # native-lib: apk/lib/libnative-lib.so Target_link_libraries (native-lib # ${log-lib} # getndk) # log Cmake is now in metadata_generation_stderr.txt or cmake_server_log, and is now in metadata_generation_stderr.txt. Or Linked_C++_Projects # can be viewed in Build, note: #[[(none) = important message; STATUS = non-important message; WARNING = CMake WARNING; AUTHOR_WARNING = CMake WARNING (dev); AUTHOR_WARNING = CMake WARNING (dev) SEND_ERROR = CMake error, continue execution, but generation steps will be skipped; FATAL_ERROR = CMake error, terminate all processing;  ]] message(STATUS "1DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message(STATUS "2DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message(STATUS "3DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message(STATUS "4DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message(STATUS "5DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message(STATUS "6DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message(STATUS "7DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message(STATUS "8DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message(STATUS "9DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message(STATUS "0DerrySuccessD>>>>>>>>>>>>>>>>>>>>>>>>>>>>") message("10 OldCmakeVersion:output.txt, NewCmakeVersion:cmake_server_log.txt") # TODO -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # TODO CMake # declare variables: Set (var 666) # set(var 666) # message("var = ${var}") # CMake all variables are strings. Unset (var) message("my_var = ${var}") # TODO CMake (lists) # TODO CMake (lists) # TODO CMake Set (list name value 1 value 2... Value N) or set(list name "value 1; Value 2; . ; Value N") set(list_var 1 2 3 4 5) All variables in CMake are string # or set(list_var2 "1; 2; 3; 4. 5") # list of strings? List_var = ${list_var}") message("list_var2 = ${list_var2}" False (0, OFF, NO, false, N, IGNORE, NOTFOUND) set(if_tap OFF) Set (elseif_tap ON) # define a variable elseif_tap, If (${if_tap}) message("if") elseif(${if_tap}) message("elseif") else(${if_tap}) # Message ("else") # endif(${if_tap}) # endif() # TODO CMake set(a "") # a STREQUAL "XXX" (a, etc. XXX) # NOT ==! While (NOT a STREQUAL "XXX") set (a "${a} x") message (" > > > > > > a = ${a} ") endwhile () # [[note: Foreach (item 1 2 3) message("1item = ${item}") endforeach(item) # endfor Foreach (item RANGE 2) # RANGE 0 1 2 message("2item = ${item}") endforeach(item) foreach(item RANGE 1 6 2) # 1 2 2 message("3item = ${item}") Endforeach (item) set(list_va3 1 2 3) # foreach(item IN LISTS ${list_va3}) Foreach (item IN LISTS list_va3) message("4item = ${item}") endforeach(item) # TODO CMake ARGV0: indicates the first argument, ARGV1, ARGV2, and so on ARGV: Function (num_method n1 n2 n3) message("call num_method method") message("n1 = ${n1}") message("n2 = ${n2}") function("call num_method method") message("n1 = ${n1}") message("n3 = ${n3}") message("ARGC = ${ARGC}") message("arg1 = ${ARGV0} arg2 = ${ARGV1} arg3 = ${ARGV2}") message("all Args = ${ARGV}") endFunction (num_method) num_method(1 2 3Copy the code

Static library and dynamic library

Static library: the code inside is copied to the main library. So you can’t see it in APK/LIBS.

Dynamic library: it will not be copied to the main library, so it can be seen under APK/LIBS.

Required and not required are based on cross-compiled libraries under Linux. Dynamic libraries compiled by CLang as will be automatically loaded

Cmake_minimum_required (VERSION 3.10.2) project("ndk28_cmake") # import CPP c SOURCE file(GLOB SOURCE ${CMAKE_SOURCE_DIR}/ CPP /*.cpp ${CMAKE_SOURCE_DIR}/ CPP /*.c) STATIC) add_library(native-lib # library name --> libnative-lib.so SHARED # CPP source file: Compile the CPP SOURCE file into libnative - lib. ${SOURCE}) # TODO so library > > > > > > > > > > > > > > > > > > > > > > > > precompiled library (have the XXX. So/XXX. A) import way of style # TODO: Directories ("${CMAKE_SOURCE_DIR}/ CPP /inc") Set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -l ${CMAKE_SOURCE_DIR}/jniLibsaaa/${CMAKE_ANDROID_ARCH_ABI}") # The third step is to link to the target_link_libraries(# native-lib is our library, # native-lib is our library, # log is our library, # automatically find the specific library, and link to the libnative-lib.so inside the fmod Libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so: libnative-lib.so Directories ("${CMAKE_SOURCE_DIR}/ CPP /inc") Add_library (fMOD SHARED IMPORTED) set_target_properties(fMOD PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibsaaa/${CMAKE_ANDROID_ARCH_ABI}/libfmod.so) add_library(fmodL SHARED IMPORTED) set_target_properties(fmodL PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibsaaa/${CMAKE_ANDROID_ARCH_ABI}/ libfmodl.so Libnative-lib is our library, so we can link to libnative-lib.so we can link to libmodl # specific links to libnative - lib. # TODO so inside)]] > > > > > > > > > > > > > > > > > > > > > > > > judge static library and dynamic library (static library will Copy directly to the pool, Directories ("${CMAKE_SOURCE_DIR}/ CPP /inc") # directories(${CMAKE_SOURCE_DIR}/ CPP /inc") Add_library (fMOD SHARED IMPORTED) set_target_properties(fMOD PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibsaaa/${CMAKE_ANDROID_ARCH_ABI}/libfmod.so) add_library(fmodL SHARED IMPORTED) set_target_properties(fmodL PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibsaaa/${CMAKE_ANDROID_ARCH_ABI}/ libfmodl.so) # >>>>>>>>>>> end ## OFF=0=false ON=1=true # set(isSTATIC OFF) set(isSTATIC ON) if(${isSTATIC}) # add_library(getndk STATIC IMPORTED System.loadLibrary("getndk"); // If it is a dynamic library, it needs to be loaded. Otherwise comment set_target_properties(getnDK PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/ CPP/libgetndK. a) message("isSTATIC == Static ") else(${isSTATIC}) # add_library(getndk SHARED IMPORTED) # loadLibrary(getndk); // If it is a dynamic library, it needs to be loaded. Otherwise, comment set_target_properties(getnDK PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/jniLibsaaa/${CMAKE_ANDROID_ARCH_ABI}/libgetndk.so) message("isSTATIC == shared") endif(${isSTATIC}) Target_link_libraries (# native-lib is our total library # native-lib is linked to the total library log # automatically find the specific library link to libnative-lib.so go to getndk # TODO The specific library is linked to libnative-lib.so. Fmod # libnative-lib.so)]] # RTMP [fmod # libnative-lib.so]] # RTMP [fmod # libnative-lib.so]] # RTMP [fmod # libnative-lib.so]] # RTMP [fmod # libnative-lib.so]] # RTMP # TODO it fully USES the source build way > > > > > > > > > > > > > > > > > > > > > > > > way dependent on the source code (have XXXC/XXX. C/XXX. C... Add_subdirectory (${CMAKE_SOURCE_DIR}/ CPP /libget) # import cmakelists. TXT from count Add_subdirectory (${CMAKE_SOURCE_DIR}/ CPP /libcount) target_link_libraries(${CMAKE_SOURCE_DIR}/ CPP /libcount Automatically find # specific libraries to link to libnative-lib.so to get # specific libraries to link to libnative-lib.so to count # specific libraries to link to libnative-lib.so)Copy the code

Four, source code construction method

# RTMP, it fully USES the source build way # TODO > > > > > > > > > > > > > > > > > > > > > > > > way dependent on the source code (have XXXC/XXX. C/XXX. C... Add_subdirectory (${CMAKE_SOURCE_DIR}/ CPP /libget) target_link_libraries(#) Libnative-lib.so libnative-lib.so libnative-lib.so libnative-lib.so libnative-lib.so libnative-lib.so libnative-lib.soCopy the code