51

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.

5 Answers 5

76

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@DiscGolfer Why you remove LOCAL_PATH from the listing?
@Narek You must define LOCAL_PATH := $(call my-dir) at the top of your Android.mk. This holds the relative path from your NDK root to the current file. So, in the LOCAL_SRC_FILES line above, I removed the LOCAL_PATH from the listing because the file paths are relative to LOCAL_PATH. Which would make the paths invalid if not removed.
What does the "wildcard" part mean? Makes sense that LOCAL_PATH will get expanded into a path, is "wildard" a predefined value? Is something supposed to be substituted for "wildcard"?
What does $(FILE_LIST:$(LOCAL_PATH)/%=%) mean?
@Delargo This removes the LOCAL_PATH from each file path in the FILE_LIST. Itbasically makes the LOCAL_SRC_FILES list contain only relative paths to your files.
|
22

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

2 Comments

To anybody using this, make sure that this definition is placed in your Makefile before any code that uses it, otherwise calls to it will silently fail.
How exclude path from FILE_LIST? And why write $(FILE_LIST:$(LOCAL_PATH)/%=%), not just $(FILE_LIST) when I assign FILE_LIST to LOCAL_SRC_FILES?
3

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))

Comments

0

Using this:

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

Comments

0
LOCAL_SRC_FILES := $(call all-cpp-files-under,$(LOCAL_PATH))

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has a few answers—including at least one that has been validated by the community. It would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.