nc-wc-ndk_pub/docs/dev_cmake.rst

122 lines
4.1 KiB
ReStructuredText

================
CMake Tool Notes
================
* https://developer.android.com/studio/projects/configure-cmake
* https://developer.android.com/ndk/guides/cmake
Shared vs Static (also how Android/Java runs native code)
----------------------------------------------------------
The term shared library is not a perfect fit regarding Android's NDK, because in many cases the .so libraries aren't actually shared between applications. It's better to classify the libraries that the NDK builds as static and dynamic.
Every Android application is a Java application, and the only entry point for the NDK code is loading it as a dynamic library and call it trough JNI.
Static libraries are an archives of compiled object files. They get bundled in other libraries at build time. Unused portions of code from static libraries are stripped by the NDK to reduce total size.
Dynamic libraries are loaded at runtime from separate files. They can contain static libraries that they are dependent on or load more dynamic libraries.
So what you actually need for Android development is at least one shared library, that will be called from Java code, and linked with it's dependencies as static libraries preferably.
Native library
--------------
* lib*library-name*.so
"native-lib" == "libnative-lib.so" on the file system
- For example, if you specify "native-lib" as the name of your shared library in the build script, CMake creates a file named libnative-lib.so. However, when loading this library in your Java or Kotlin code, use the name you specified in the CMake build script:
static {
System.loadLibrary("native-lib");
}
Compiling C/C++ Code
--------------------
* This code will find a native NDK library (provided by Android OS), and assign the path to the
variable log-lib
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log )
* This code defines the linking process of the C/C++ Build
# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the log library to the target library.
${log-lib} )
* Some Android libraries are source files, include them like this
add_library( app-glue
STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
* Add a prebuilt library
add_library( imported-lib
SHARED
IMPORTED )
# CMake needs to find the header files for the library
include_directories( imported-lib/include/ )
set_target_properties( # Specifies the target library.
imported-lib
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
imported-lib/src/${ANDROID_ABI}/libimported-lib.so )
Example File ("CMakeLists.txt")
===============================
cmake_minimum_required(VERSION 3.4.1)
add_library( # Specifies the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
add_library( app-glue
STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
# Specifies a path to native header files.
include_directories(src/main/cpp/include/)
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log )
# You need to link static libraries against your shared native library.
target_link_libraries( native-lib app-glue ${log-lib} )