android中c++標準的選擇
C++ Standard
指定編譯庫的環境,其中Toolchain Default使用的是默認的CMake環境;C++ 11也就是C++環境。兩種環境都可以編庫,至于區別,后續會跟進,當前博文使用的是CMake環境。
Exceptions Support
如果選中復選框,則表示當前項目支持C++異常處理,如果支持,在項目Module級別的build.gradle
文件中會增加一個標識 -fexceptions
到cppFlags
屬性中,并且在so庫構建時,gradle會把該屬性值傳遞給CMake進行構建。
Runtime Type Information Support
同理,選中復選框,項目支持RTTI,屬性cppFlags
增加標識-frtti
CMakeLists.txt的配置
CMakeLists.txt 用于配置jni項目屬性,主要用于聲明CMake版本 so庫名稱 C/Cpp文件路徑等信息。
注釋
和bash中一樣,使用“#”作為一行的注釋。
cmake版本聲明
cmake_minimum_required(VERSION 3.4.1)
添加編譯目標add_library()
配置庫信息,庫的名字,動態庫或靜態庫,依賴的源文件
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp
src/main/cpp/test.cpp)
STATIC:靜態庫,是目標文件的歸檔文件,在鏈接其它目標的時候使用。
SHARED:動態庫,會被動態鏈接,在運行時被加載。
MODULE:模塊庫,是不會被鏈接到其它目標中的插件,但是可能會在運行時使用dlopen-系列的函數動態鏈接。
配置頭文件路徑include_directories()
include_directories("src/main/cpp")
查找鏈接庫find_library
在指定目錄下搜索一個庫, 保存在變量log-lib中,如果沒有指定路徑,則使用默認系統路徑
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
添加鏈接庫 target_link_libraries
添加鏈接庫,相同于指定-l參數
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )