我们在Android平台写c/c++程序的时候需要用到Android.mk(Makefile),一般用来编译c/c++源码、引用第三方头文件和库,生成程序所需的so文件。下面是一个cocos2d-x游戏的Android.mk(删除了一些重复的东西),一般默认在jni目录下:
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
87
88
|
#1
LOCAL_PATH := $(call my-
dir
)
#2 自定义了一个all_cpp_files_recursively函数,递归遍历返回给定目录下所有C++源文件。
all_cpp_files_recursively = \
$(
eval
src_files = $(wildcard $1/*.cpp)) \
$(
eval
src_files = $(src_files:$(LOCAL_PATH)/%=%))$(src_files) \
$(
eval
item_all = $(wildcard $1/*)) \
$(foreach item, $(item_all) $(),\
$(
eval
item := $(item:%.cpp=%)) \
$(call all_cpp_files_recursively, $(item))\
)
#3 自定义了一个all_c_files_recursively 函数,递归遍历返回给定目录下所有C源文件。
all_c_files_recursively = \
$(
eval
src_files = $(wildcard $1/*.c)) \
$(
eval
src_files = $(src_files:$(LOCAL_PATH)/%=%))$(src_files) \
$(
eval
item_all = $(wildcard $1/*)) \
$(foreach item, $(item_all) $(),\
$(
eval
item := $(item:%.c=%)) \
$(call all_c_files_recursively, $(item))\
)
#4 声明一个预编译库的模块:共享库
include $(CLEAR_VARS)
LOCAL_MODULE := mytt
LOCAL_SRC_FILES := prebuilt
/armeabi/libmytt
.so
LOCAL_LDLIBS:= -L$(SYSROOT)
/usr/lib
-llog
include $(PREBUILT_SHARED_LIBRARY)
#5 声明一个预编译库的模块:共享库
include $(CLEAR_VARS)
LOCAL_MODULE := myts
LOCAL_SRC_FILES := prebuilt
/armeabi/libmyts
.so
LOCAL_LDLIBS:= -L$(SYSROOT)
/usr/lib
-llog
include $(PREBUILT_SHARED_LIBRARY)
#6 声明一个预编译库的模块:静态库
include $(CLEAR_VARS)
LOCAL_MODULE := mycs
LOCAL_SRC_FILES := ../..
/Classes/libtgcpapi/android/libmycs
.a
include $(PREBUILT_STATIC_LIBRARY)
#7 共享库模块west_shared
include $(CLEAR_VARS)
LOCAL_MODULE := west_shared
LOCAL_MODULE_FILENAME := libwest
#8 将要编译打包到模块west_shared中的c/c++源码文件
LOCAL_SRC_FILES := $(call all_cpp_files_recursively,$(LOCAL_PATH)
/west
)
LOCAL_SRC_FILES += $(call all_cpp_files_recursively,$(LOCAL_PATH)/../..
/Classes
)
LOCAL_SRC_FILES += $(call all_c_files_recursively,$(LOCAL_PATH)/../..
/Classes
)
#9 头文件的搜索路径
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../..
/Classes
\
$(LOCAL_PATH)/../..
/Classes/
子目录 \
......
$(LOCAL_PATH)
/west
\
$(LOCAL_PATH)/../../../其他第三方库
#10 模块west_shared链接时需要使用的静态库
LOCAL_STATIC_LIBRARIES := mycs
# 模块west_shared运行时依赖的共享库,源码中调用了其暴露的接口,所以链接时就需要,否则会出错。
LOCAL_SHARED_LIBRARIES := myts
#11 跟LOCAL_STATIC_LIBRARIES一样,只不过包含了静态库的所有的源代码。
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_lua_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
#12 存在于系统目录下本模块需要连接的库。
LOCAL_LDFLAGS+= -Xlinker --allow-multiple-definition
#13 表示编译成共享库
include $(BUILD_SHARED_LIBRARY)
#14 编译模块时要使用的附加的链接器选项
#LOCAL_LDLIBS:=
# 将一个新的路径xxxx加入NDK_MODULE_PATH变量
$(call
import
-add-path,$(LOCAL_PATH)/../../../)
$(call
import
-add-path,$(LOCAL_PATH)/../../..
/cocos2dx/platform/third_party/android/prebuilt
)
# 导入外部模块提供的.mk文件
$(call
import
-module,cocos2dx)
$(call
import
-module,CocosDenshion
/android
)
$(call
import
-module,scripting
/lua/proj
.android)
$(call
import
-module,extensions)
|
#1:需要了解的知识点(LOCAL_PATH、:=和=、call函数、my-dir)。
(1)定义了LOCAL_PATH 变量,下面的一些函数和变量依赖于它,记住:LOCAL_PATH 必须放到所有include $(CLEAR_VARS)之前定义。
如果想要系统的学习Makefile和了解Android.mk,可参考: