Cmake (cmake)

Cmake Use tutorial (1) – start

Cmake Tutorial 2 – Adding libraries

Cmake use tutorial (3) – installation, testing, system self – check

Cmake Use tutorial (4) – file generator

Cmake Use tutorial (5) -cpack generate installation package

Cmake Use tutorial (6) – lousy syntax

Cmake Use Tutorial (7) – Processes and loops

Cmake: Macro and Function

Cmake Use Tutorial (9) – About android cross compilation

Cmake Tutorial (10) – About File

This series of articles was translated from the official Cmake tutorial: CMake Tutorial.

Example program address: github.com/rangaofei/t…

It will not stop at the official tutorial. I as an Android developer, is really no Linux C program development experience, hope big guys forgive. The tutorial is done on MacOS, and I’ve tested most Linux as well, so IF there are special instructions, I’ll note them. This tutorial is based on cmake-3.10.2 and assumes that you have cmake installed.

Introduction to the

This article does not explain how to compile, can you refer to my other article

Shell scripts generate both the android full ABI dynamic and static libraries

Cross compiling with cmake is fairly simple, and can be written once and run every time.

CMake uses toolchain to compile, link library and create archives, and other tasks to drive builds. The available Toolchain utility is determined by the language enabled. In the normal version, CMake automatically determines the main toolchain to use based on the system defaults. In a cross-compilation scenario, information about the compiler and utility paths can be used to specify the Toolchain file.

If we have already written our toolchain file, add -dcmake_toolchain_file =path/to/file at build time, and the cmake system will automatically use the executed toolchain.cmake file.

There are two ways to cross-compile on Android. One is to compile with the NDK, and the other is to compile with your own tool chain. I feel like I only know how to compile with the NDK, but I won’t go into the other one because I don’t.

Here’s how to build both makefile and Ninja cross-builds.

Variable declaration

There are a few special variables that need to be set:

  1. CMAKE_SYSTEM_NAME

    Set this variable to Android when compiling for the Android platform. And CMAKE_SYSTEM_VERSION must be specified.

  2. CMAKE_SYSTEM_VERSION

    Set the Android API level. If this value is not set, it will be determined in one of two ways:

    • CMAKE_ANDROID_APIIf the API level is set, the API level is used
    • CMAKE_SYSROOTIf the API level is set, the API level is used
    • Levele, the latest API in the NDK, is used
  3. CMAKE_ANDROID_ARCH_ABI

    Arm64-v8a armeabi-v7a Armeabi-v6 Armeabi MIPS mips64 x86 x86_64. This is awesome. Set the ABI architecture. If not, use the default ArmeABI. Setting this variable will automatically set CMAKE_ANDROID_ARCH, without manual setting.

  4. CMAKE_ANDROID_NDK

    Use this to set the path of the NDK. It must be an absolute path, to the root directory of the NDK.

  5. CMAKE_ANDROID_NDK_DEPRECATED_HEADERS

    Set to true to use deprecated headers at each API level rather than a uniform header. The default is false and the header file is uniform.

  6. CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION

    Set the version of the NDK compilation chain, or if not, use the latest one.

  7. CMAKE_ANDROID_STL_TYPE This is awesome. There are several values:

    none No C++ Support

    system Minimal C++ without STL

    gabi++_static GAbi++ Static

    gabi++_shared GAbi++ Shared

    gnustl_static GNU libstdc++ Static

    gnustl_shared GNU libstdc++ Shared

    c++_static LLVM libc++ Static

    c++_shared LLVM libc++ Shared

    stlport_static STLport Static

    stlport_shared STLport Shared

    The default is gnustl_static.

  8. CMAKE_<LANG>_ANDROID_TOOLCHAIN_PREFIX

    Automatically generated, absolute path prefix.

  9. CMAKE_

    _ANDROID_TOOLCHAIN_SUFFIX Automatically generated, absolute path suffix.

Build order

  1. If I setCMAKE_ANDROID_NDK, the NDK path specified by this variable is used
  2. If I setCMAKE_ANDROID_STANDALONE_TOOLCHAINIf the variables in 1 are not set, the separate toolchain is used to compile.
  3. If I setCMAKE_SYSROOT,1 and 2 are not set, and the path format is<ndk>/platforms/android-<api>/arch-<arch>, is equivalent to settingCMAKE_ANDROID_NDKAnd the NDK for that path will be used.
  4. If I setCMAKE_SYSROOT, 1, 2, and 3 are not set, and the path format is<standalone-toolchain>/sysroot, is equivalent to settingCMAKE_ANDROID_STANDALONE_TOOLCHAINAnd compile using the tool chain.
  5. If I setANDROID_NDKIf 1, 2, 3, 4 is not set, then 1 is set and the NDK is used.
  6. If I setANDROID_STANDALONE_TOOLCHAINIf 1, 2, 3, 4, 5 is not set, 2 is set and the tool chain is used.
  7. Suppose environment variables are setANDROID_NDK_ROOTorANDROID_NDK, 1, 2, 3, 4, 5, 6 is equivalent to setting 1 and using the NDK.
  8. If I setANDROID_STANDALONE_TOOLCHAINIf 1, 2, 3, 4, 5, 6, 7 is not set, 2 is set and the tool chain is used.
  9. Error will be reported if none is set.

Compile the way

The Settings are as follows:

set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 21) # API level
set(CMAKE_ANDROID_ARCH_ABI arm64-v8a)
set(CMAKE_ANDROID_NDK /path/to/android-ndk)
set(CMAKE_ANDROID_STL_TYPE gnustl_static)
Copy the code

If you don’t want to write a toolchain.cmake file, you can also enter arguments directly on the command line to complete the build:

cmake .. /src \ -DCMAKE_SYSTEM_NAME=Android \ -DCMAKE_SYSTEM_VERSION=21 \ -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \ -DCMAKE_ANDROID_NDK=/path/to/android-ndk \ -DCMAKE_ANDROID_STL_TYPE=gnustl_staticCopy the code

The effect is the same as above.