Android.mk中添加目录下所有cpp文件

来源:互联网 发布:sql 带游标的存储过程 编辑:程序博客网 时间:2024/05/16 14:25

Android开发中,经常遇到需要在andoid.mk文件中包含特定的cpp文件(或c文件)

LOCAL_SRC_FILES := hellocpp/main.cpp \    ../../Classes/AppDelegate.cpp \    ../../Classes/ClipingNodeLayer.cpp\    ../../Classes/MainScene.cpp\    ../../Classes/MenuTab.cpp

改为

# 遍历目录及子目录的函数define walk$(</span>wildcard <span class="variable" style="color:rgb(0,128,128)">$(1)) $(</span><span class="keyword" style="font-weight:bold">foreach</span> e, <span class="variable" style="color:rgb(0,128,128)">$(wildcard $(</span><span class="number" style="color:rgb(0,153,153)">1</span>)/<span class="variable" style="color:rgb(0,128,128)">*)</span>, <span class="variable" style="color:rgb(0,128,128)">$(call walk, $(e)))endef# 遍历Classes目录ALLFILES = $(</span>call walk, <span class="variable" style="color:rgb(0,128,128)">$(LOCAL_PATH)/../../Classes)FILE_LIST := hellocpp/main.cpp# 从所有文件中提取出所有.cpp文件FILE_LIST += $(</span>filter <span class="variable" style="color:rgb(0,128,128)">%.</span>cpp, <span class="variable" style="color:rgb(0,128,128)">$(ALLFILES))LOCAL_SRC_FILES := $(</span>FILE_LIST:<span class="variable" style="color:rgb(0,128,128)">$(LOCAL_PATH)/%=%)

另有一例

define all-cpp-files-under  $(patsubst ./%,%, \    $(shell cd $(LOCAL_PATH) ; \      find $(1) -name "*.cpp" -and -not -name ".*" -and -not -name "CCEditBoxImplWindow.cpp") \   )  endef  define all-subdir-cpp-files  $(call all-cpp-files-under,.)  endef  LOCAL_SRC_FILES := $(call all-subdir-cpp-files)  

使用这个方法可以遍历子目录所有.cpp文件,替换find的参数可以实现遍历和过滤任意文件。 android.mk编写变得非常简洁和方便,无需再维护文件列表了。

另附一个更加简单的宏,可以实现遍历一个目录下的所有文件(但是不会递归调用)

LOCAL_SRC_FILES := $(</span>wildcard <span class="variable" style="color:rgb(0,128,128)">$(LOCAL_PATH)/../*.cpp) 

通过wildcard可以进行文件遍历,如果是单目录结构,通过这个同样可以达到非常简洁的效果。如果是c++代码的话(*.cpp文件),需要使用下面的方式,否则可能找不到文件:

FILE_LIST := $(</span>wildcard <span class="variable" style="color:rgb(0,128,128)">$(LOCAL_PATH)/../*.cpp)  LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)  

加强版本(遍历所有文件,但是忽略某一个目录的文件),通过-prune可以指定忽略 “LogicLayer”这个目录

define all-cpp-files-under    $(patsubst ./%,%, \    $(shell cd $(LOCAL_PATH) ; \      find $(1) -name LogicLayer -prune -o -name "*.cpp" -and -not -name ".*") \    )  endef LOCAL_SRC_FILES := $(call all-subdir-cpp-files)
原创粉丝点击