ABAP Netweaver, Hybris Commerce和SAP 云平臺的登錄認證
1370
2025-04-01
文章目錄
一、Android NDK 構(gòu)建腳本
二、CMake 構(gòu)建腳本示例
三、CMake 命令手冊
1、CMake 腳本命令
2、CMake 工程命令
該系列博客的應(yīng)用場景是 android studio 下 NDK 編程 , 使用 CMake 構(gòu)建 C/C++ 工程 ;
一、Android NDK 構(gòu)建腳本
Android 中使用 NDK 編譯 C/C++ 源碼有兩種方式 , 參考 Android 官網(wǎng) https://developer.android.google.cn/ndk/guides ;
方式一 : 使用 ndk-build 腳本 , 包括 Android.mk 和 Application.mk 兩個腳本 , 詳細的細節(jié)參考如下鏈接
ndk-build
Android.mk
Application.mk
使用預構(gòu)建的庫
老的 Android 開發(fā)環(huán)境使用的是這種方式編譯 C/C++ 源碼 ;
方式二 : 使用 CMake 構(gòu)建腳本 CMakeLists.txt 編譯 C/C++ 源碼 , 參考如下鏈接
CMake
這是目前 Google 官方推薦的方式 ;
二、CMake 構(gòu)建腳本示例
下面的構(gòu)建腳本中用到了很多命令 , 如
cmake_minimum_required
add_library
set_target_properties
message
find_library
set
target_link_libraries
上述命令都是 CMake 命令 , 可以在 CMake 命令文檔中查詢 , 當前 CMake 中有 47 47 47 個腳本命令 , 46 46 46 個工程命令 ;
CMake 構(gòu)建腳本示例 :
# 指定 CMake 最低版本 cmake_minimum_required(VERSION 3.4.1) # 設(shè)置函數(shù)庫編譯 add_library( # 參數(shù) 1 : 設(shè)置生成的動態(tài)庫名稱 native-lib # 參數(shù) 2 : 設(shè)置生成的函數(shù)庫類型 : ① 靜態(tài)庫 STATIC ② 動態(tài)庫 SHARED SHARED # 參數(shù) 3 : 配置要編譯的源文件 native-lib.cpp) # 引入靜態(tài)庫 # ① 參數(shù) 1 ( add ) : 設(shè)置引入的靜態(tài)庫名稱 # ② 參數(shù) 2 ( SHARED ) : 設(shè)置引入的函數(shù)庫類型 : ① 靜態(tài)庫 STATIC ② 動態(tài)庫 SHARED # ③ 參數(shù) 3 ( IMPORTED ) : 表示引入第三方靜態(tài)庫 , 導入靜態(tài)庫 , 相當于預編譯靜態(tài)庫 # 后續(xù)還需要設(shè)置導入路徑 , 配合該配置使用 add_library( # 設(shè)置引入的靜態(tài)庫名稱 add # 設(shè)置引入的函數(shù)庫類型為靜態(tài)庫 STATIC # 表示引入第三方靜態(tài)庫 IMPORTED) # 設(shè)置上述靜態(tài)庫的導入路徑 # 設(shè)置目標屬性參數(shù) : # ① 參數(shù) 1 ( add ) : 要設(shè)置哪個函數(shù)庫的屬性 # ② 參數(shù) 2 ( PROPERTIES ) : 設(shè)置目標屬性 # ③ 參數(shù) 3 ( IMPORTED_LOCATION ) : 設(shè)置導入路徑 # ④ 參數(shù) 4 : 配置靜態(tài)庫的文件路徑 set_target_properties( # 設(shè)置目標 add # 設(shè)置屬性 PROPERTIES # 導入路徑 IMPORTED_LOCATION # ${CMAKE_SOURCE_DIR} 是本 CMakeList.txt 構(gòu)建腳本的路徑 , 是 CMake 工具內(nèi)置的變量 # Android CMake 也內(nèi)置了一些變量 , 如 ANDROID_ABI ${CMAKE_SOURCE_DIR}/../jniLibs/armeabi-v7a/libadd.a) # 打印日志信息 # ${ANDROID_ABI} 的作用是獲取當前的 CPU 指令集架構(gòu) # 當本次編譯 armeabi-v7a CPU 架構(gòu)時 , ${ANDROID_ABI} 值為 armeabi-v7a # 當本次編譯 x86 CPU 架構(gòu)時 , ${ANDROID_ABI} 值為 x86 message("CMAKE_SOURCE_DIR : ${CMAKE_SOURCE_DIR}, ANDROID_ABI : ${ANDROID_ABI}") # 到預設(shè)的目錄查找 log 庫 , 將找到的路徑賦值給 log-lib # 這個路徑是 NDK 的 ndk-bundle\platforms\android-29\arch-arm\usr\lib\liblog.so # 不同的 Android 版本號 和 CPU 架構(gòu) 需要到對應(yīng)的目錄中查找 , 此處是 29 版本 32 位 ARM 架構(gòu)的日志庫 find_library( log-lib log) # 設(shè)置變量 # CMAKE_CXX_FLAGS 表示會將 C++ 的參數(shù)傳給編譯器 # CMAKE_C_FLAGS 表示會將 C 參數(shù)傳給編譯器 # 參數(shù)設(shè)置 : 傳遞 CMAKE_CXX_FLAGS C+= 參數(shù)給編譯器時 , 在 該參數(shù)后面指定庫的路徑 # CMAKE_SOURCE_DIR 指的是當前的文件地址 # -L 參數(shù)指定動態(tài)庫的查找路徑 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/../jniLibs/armeabi-v7a") # 鏈接函數(shù)庫 # 參數(shù) 1 : 本構(gòu)建腳本要生成的動態(tài)庫目 標 # 參數(shù) 2 ~ ... : 后面是之前預編譯的動態(tài)庫或靜態(tài)庫 , 或引入的動態(tài)庫 target_link_libraries( native-lib # 表示 編譯 native-lib 模塊, 要鏈接 add 模塊 add ${log-lib})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
從之前的一篇博客 【Android NDK 開發(fā)】android studio 使用 CMake 導入靜態(tài)庫 ( CMake 簡介 | 構(gòu)建腳本路徑配置 | 引入靜態(tài)庫 | 指定靜態(tài)庫路徑 | 鏈接動態(tài)庫 ) 復制來的 ;
三、CMake 命令手冊
CMake 命令分為三種 ,
腳本命令 ,
工程命令 ,
CTest 命令 ;
1、CMake 腳本命令
CMake 有 47 47 47 個腳本命令 , 這些命令在任何時候都可以使用 , 參考 cmake-commands(7) : Scripting Commands 文檔 ;
47 47 47 個腳本命令如下 :
break cmake_host_system_information cmake_language cmake_minimum_required cmake_parse_arguments cmake_policy configure_file continue else elseif endforeach endfunction endif endmacro endwhile execute_process file find_file find_library find_package find_path find_program foreach function get_cmake_property get_directory_property get_filename_component get_property if include include_guard list macro mark_as_advanced math message option return separate_arguments set set_directory_properties set_property site_name string unset variable_watch while
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2、CMake 工程命令
CMake 中有 46 46 46 個工程命令 , 一般在 CMake 工程中才可以使用上述工程命令 ;
46 46 46 個工程命令如下 , 詳細的命令細節(jié)參考 cmake-commands(7) : Project Commands 文檔 ;
add_compile_definitions add_compile_options add_custom_command add_custom_target add_definitions add_dependencies add_executable add_library add_link_options add_subdirectory add_test aux_source_directory build_command create_test_sourcelist define_property enable_language enable_testing export fltk_wrap_ui get_source_file_property get_target_property get_test_property include_directories include_external_msproject include_regular_expression install link_directories link_libraries load_cache project remove_definitions set_source_files_properties set_target_properties set_tests_properties source_group target_compile_definitions target_compile_features target_compile_options target_include_directories target_link_directories target_link_libraries target_link_options target_precompile_headers target_sources try_compile try_run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
在 Android Studio 中的 CMake 構(gòu)建腳本 CMakeLists.txt 中使用到的所有 CMake 命令都來自上述腳本命令和工程命令 , 如 add_library , find_library , target_link_libraries 等 ;
Android Studio 中的 CMakeLists.txt 構(gòu)建腳本中常用的命令 :
cmake_minimum_required
add_library
set_target_properties
message
find_library
set
target_link_libraries
Android ARM
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應(yīng)法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應(yīng)法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。