{"id":983,"date":"2023-03-12T00:17:08","date_gmt":"2023-03-11T16:17:08","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=983"},"modified":"2023-04-29T13:24:25","modified_gmt":"2023-04-29T05:24:25","slug":"android-mk-add-all-source-files-in-directory","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/android-mk-add-all-source-files-in-directory\/","title":{"rendered":"Android.mk\u6dfb\u52a0\u76ee\u5f55\u4e2d\u6240\u6709\u6e90\u6587\u4ef6"},"content":{"rendered":"<p>Android NDK\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230\u9700\u8981\u5728andoid.mk\u6587\u4ef6\u4e2d\u5305\u542b\u7279\u5b9a\u7684cpp\u6587\u4ef6\uff08\u6216c\u6587\u4ef6\uff09<\/p>\n<pre><code>LOCAL_SRC_FILES := main.cpp \\\n    mylib\/src1.cpp \\\n    mylib\/Classes\/src2.cpp\\\n    mylib\/Classes\/src3.cpp\\\n    mylib\/Classes\/src4.cpp<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u6539\u4e3a<\/p>\n<pre><code># \u904d\u5386\u76ee\u5f55\u53ca\u5b50\u76ee\u5f55\u7684\u51fd\u6570\ndefine walk\n$(wildcard $(1)) $(foreach e, $(wildcard $(1)\/*), $(call walk, $(e)))\nendef\n\n# \u904d\u5386mylib\u76ee\u5f55\nMYLIB_FILES = $(call walk, $(LOCAL_PATH)\/mylib)\nSRC_FILES := main.cpp\n# \u4ece\u6240\u6709\u6587\u4ef6\u4e2d\u63d0\u53d6\u51fa\u6240\u6709.cpp\u6587\u4ef6\nSRC_FILES += $(filter %.cpp, $(MYLIB_FILES))\n\nLOCAL_SRC_FILES := $(SRC_FILES:$(LOCAL_PATH)\/%=%)<\/code><\/pre>\n<p>\u53e6\u6709\u4e00\u4f8b<\/p>\n<pre><code>define all-cpp-files-under\n$(patsubst .\/%,%, \\\n  $(shell cd $(LOCAL_PATH) ; \\\n    find $(1) -name &quot;*.cpp&quot; or -name &quot;*.c&quot; -and -not -name &quot;.*&quot; -and -not -name &quot;exclude.cpp&quot;) \\\n )\nendef\n\ndefine all-subdir-cpp-files\n$(call all-cpp-files-under,.)\nendef\n\nLOCAL_SRC_FILES := $(call all-subdir-cpp-files)<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u904d\u5386\u5b50\u76ee\u5f55\u6240\u6709 <code>.cpp\/.c<\/code> \u6587\u4ef6\uff0c\u66ff\u6362find\u7684\u53c2\u6570\u53ef\u4ee5\u5b9e\u73b0\u904d\u5386\u548c\u8fc7\u6ee4\u4efb\u610f\u6587\u4ef6\u3002Android.mk\u7f16\u5199\u53d8\u5f97\u975e\u5e38\u7b80\u6d01\u548c\u65b9\u4fbf\uff0c\u65e0\u9700\u518d\u7ef4\u62a4\u6587\u4ef6\u5217\u8868\u3002<\/p>\n<p>\u53e6\u9644\u4e00\u4e2a\u66f4\u52a0\u7b80\u5355\u7684\u5b8f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u904d\u5386\u4e00\u4e2a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\uff08\u4f46\u662f\u4e0d\u4f1a\u9012\u5f52\u8c03\u7528\uff09<\/p>\n<pre><code>LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)\/mylib\/*.cpp)<\/code><\/pre>\n<p>\u901a\u8fc7wildcard\u53ef\u4ee5\u8fdb\u884c\u6587\u4ef6\u904d\u5386\uff0c\u5982\u679c\u662f\u5355\u76ee\u5f55\u7ed3\u6784\uff0c\u901a\u8fc7\u8fd9\u4e2a\u540c\u6837\u53ef\u4ee5\u8fbe\u5230\u975e\u5e38\u7b80\u6d01\u7684\u6548\u679c\u3002\u5982\u679c\u662fc++\u4ee3\u7801\uff08*.cpp\u6587\u4ef6\uff09\uff0c\u9700\u8981\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u5f0f\uff0c\u5426\u5219\u53ef\u80fd\u627e\u4e0d\u5230\u6587\u4ef6\uff1a<\/p>\n<pre><code>FILE_LIST := $(wildcard $(LOCAL_PATH)\/mylib\/*.cpp)\nLOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)\/%=%)<\/code><\/pre>\n<p>\u52a0\u5f3a\u7248\u672c\uff08\u904d\u5386\u6240\u6709\u6587\u4ef6\uff0c\u4f46\u662f\u5ffd\u7565\u67d0\u4e00\u4e2a\u76ee\u5f55\u7684\u6587\u4ef6\uff09\uff0c\u901a\u8fc7-prune\u53ef\u4ee5\u6307\u5b9a\u5ffd\u7565 &quot;exclude_dir&quot; \u8fd9\u4e2a\u76ee\u5f55<\/p>\n<pre><code>define all-cpp-files-under\n  $(patsubst .\/%,%, \\\n  $(shell cd $(LOCAL_PATH) ; \\\n    find $(1) -name exclude_dir -prune -o -name &quot;*.cpp&quot; -and -not -name &quot;.*&quot;) \\\n  )\nendef\n\nLOCAL_SRC_FILES := $(call all-subdir-cpp-files)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Android NDK\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230\u9700\u8981\u5728andoid.mk\u6587\u4ef6\u4e2d\u5305\u542b\u7279\u5b9a\u7684cpp\u6587\u4ef6\uff08\u6216c\u6587\u4ef6\uff09 LOCA [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-983","post","type-post","status-publish","format-standard","hentry","category-android-ndk"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=983"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/983\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}