Android.mk, include all cpp files

AndroidAndroid Ndk

Android Problem Overview


I'm trying to build an Android project using the ndk, but I have run into some troubles.

Here's the Android.mk file that works:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := mylib
LOCAL_CFLAGS    := -Werror
LOCAL_SRC_FILES := main.cpp, Screen.cpp, ScreenManager.cpp  
LOCAL_LDLIBS    := -llog

include $(BUILD_SHARED_LIBRARY)

Is there a way that allows me to specify all the *.cpp files in the directory, without listing them manually under LOCAL_SRC_FILES?

So far I tried using LOCAL_SRC_FILES = $(wildcard *.cpp), but it did now work, it seems that no files get selected.

Android Solutions


Solution 1 - Android

You could try something like this...

FILE_LIST := $(wildcard $(LOCAL_PATH)/[DIRECTORY]/*.cpp)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)

... Change [DIRECTORY] to the actual directory of the files. If they are in the same directory as your .mk file then remove that part. Create the FILE_LIST variable to find all of the .cpp files under the [DIRECTORY] directory. Then use it in the file listing. The LOCAL_SRC_FILES line will then remove the LOCAL_PATH from the listing.

Solution 2 - Android

I've been using this script for my Android.mk saved me so much time!

#traverse all the directory and subdirectory
define walk
  $(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef

#find all the file recursively under jni/
ALLFILES = $(call walk, $(LOCAL_PATH))
FILE_LIST := $(filter %.cpp, $(ALLFILES))

LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)

Here is the gist

Solution 3 - Android

How about like this:

LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/*.cpp))

If you'd be afraid that expansion of * contains $(LOCAL_PATH)/, it might be OK:

LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/./,,$(wildcard $(LOCAL_PATH)/./*.cpp))

Solution 4 - Android

Using this:

LOCAL_SRC_FILES += $($(wildcard $(LOCAL_PATH)/*.cpp):$(LOCAL_PATH)/%=%)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongq3View Question on Stackoverflow
Solution 1 - AndroidDRiFTyView Answer on Stackoverflow
Solution 2 - AndroidNiTe LuoView Answer on Stackoverflow
Solution 3 - Androiduser2356722View Answer on Stackoverflow
Solution 4 - Android赵国涛View Answer on Stackoverflow