CMake 建構腳本是一個純文字檔案,您必須将其命名為
CMakeLists.txt
,并在其中包含 CMake 建構您的 C/C++ 庫時需要使用的指令。如果您的原生源代碼檔案還沒有 CMake 建構腳本,您需要自行建立一個,并在其中包含适當的 CMake 指令。
添加so庫
可以用你寫的cpp源檔案生成一個so
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 )
也可以直接導入已有的庫(import)
add_library( imported-lib
SHARED
IMPORTED )
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 )
使用
include_directories()
指令并包含相應頭檔案的路徑,使CMake 能夠在編譯時定位你的頭檔案:
設定接口庫對第三方庫的連結,其中第一個參數為接口庫,後面參數為第三方庫
一個簡單的CMakeList.txt 例子
cmake_mini_required(VERSION 3.4.1) #定義cmake 支援的版本
project(my_proj) #項目名
include_directories(include) #頭檔案路徑
aux_source_directory(src DIR_SOURCE) #源檔案目錄,DIR_SOURCE 為定義的變量
set(SRC_FILE_PATH ${DIR_SOURCE}) #設定環境變量,編譯用到的源檔案都要放到這裡
add_executable(my_proj ${SRC_FILE_PATH}) #設定可執行源檔案編譯成的可執行檔案名
未完待續........