Android.mk中LOCAL_SRC_FILES需要罗列出所有参与编译的文件,这样在.cpp文件少的时候还可以一个一个添加,当有几百个文件的时候会十分的痛苦!
我们下看看TestCpp工程中的Android.mk文件:
这只是节选的一部分,大概只有50个左右吧,除数量多之外让我们看看下面的情况:
1改变了其中一个文件名就得修改mk文件,痛苦之处显而易见,而且会重新编译整个工程!
2.手工添加时因为失误多了空格,少了\之类事情很常见,得重新编译后才能发现问题!
初级版(参考:http://blog.csdn.net/qq634416025/article/details/8904466)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hellocpp_shared
LOCAL_MODULE_FILENAME := libhellocpp
FILE_LIST := hellocpp/main.cpp
FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,cocos2dx)
进化版
(参考: http://blog.csdn.net/teng_ontheway/article/details/16951819
)
但这这个做法还是有缺陷,就是如果Classes有子文件夹的话还是得在添加上去的,如下:
#FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp)
#FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/datapacker/*.cpp)
#FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/platform/*.cpp)
上边的解决方案用到了
wildcard
这个关键子,再加利用,便有了下面的这个:
# 遍历目录及子目录的函数
define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef
# 遍历Classes目录
ALLFILES = $(call walk, $(LOCAL_PATH)/../../Classes)
FILE_LIST := hellocpp/main.cpp
# 从所有文件中提取出所有.cpp文件
FILE_LIST += $(filter %.cpp, $(ALLFILES))
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
# 修改include路径
FILE_INCLUDES := $(shell find $(LOCAL_PATH)/../../Classes -type d)
LOCAL_C_INCLUDES := $(FILE_INCLUDES)
这样我们只用在项目最开始的时候改变Android.mk文件就以后再也不用碰了!
下面是子龙大大的Android.mk脚本:
https://gist.github.com/andyque/6060595
内容我也拷贝过来了
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_shared
LOCAL_MODULE_FILENAME := libgame
#traverse all the directory and subdirectory
define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef
#traverse Classes Directory
ALLFILES = $(call walk, $(LOCAL_PATH)/../../Classes)
FILE_LIST := hellocpp/main.cpp
FILE_LIST += $(filter %.cpp, $(ALLFILES))
FILE_INCLUDES := $(shell find $(LOCAL_PATH)/../../Classes -type d)
#source file will be compiled
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../external/Box2D/ \
$(FILE_INCLUDES) \
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static box2d_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,CocosDenshion/android) \
$(call import-module,cocos2dx) \
$(call import-module,extensions) \
$(call import-module,external/Box2D)