Robin
Robin

Reputation: 10368

How to replace .c and .cpp file together?

I am currently have an Android.mk file. For some requirement I need to write a standard GNU make file to build the same program.

As you know in Android native build, we simply put all source files together like

LOCAL_SRC_FILES := a.c b.c d.cpp e.cpp

Now I want to do something in Makefile like:

OBJ = $(LOCAL_SRC_FILES: .c=.o)

This will only transform .c files with .o object targets. How can I combine the condition ".c or .cpp" together?

Upvotes: 2

Views: 3347

Answers (3)

MadScientist
MadScientist

Reputation: 100916

You could use basename:

OBJ := $(addsuffix .o,$(basename $(LOCAL_SRC_FILES)))

(strips off the suffix of each file in LOCAL_SRC_FILES then adds .o to the end)

Upvotes: 6

user4207183
user4207183

Reputation:

Doing it in two steps:

SRC := main.c hello.cpp
OBJ := $(SRC:.c=.o)
OBJ := $(OBJ:.cpp=.o)

Upvotes: 2

Robin
Robin

Reputation: 10368

I think I am too busy to forget that I can just achieve this target by execute this function twice.

TMP_OBJ = $(LOCAL_SRC_FILES: .c=.o)
OBJ = $(TMP_OBJ: .cpp=.o)

Sorry for this silly question.

Upvotes: 8

Related Questions