Search code examples
makefilegnufile-extension

Make: Change all extensions to .o


I have a makefile, and in that I have an array called SOURCES. In this array, there are a c files and there are c++ files. However I want to change every path's extension in that array, no matter whether it is c or c++ to .o. How could I do this? I know that to change one extension to another I could do this: OBJECTS=$(SOURCES:.c=.o), but I want to do this for c++ files as well.


Solution

  • A couple different options.

    Use $(filter)/$(filter-out) and do the substitution twice:

    OBJECTS := $(filter %.o,$(SOURCES:.c=.o))
    OBJECTS += $(filter %.o,$(SOURCES:.cpp=.o))
    

    Or, if you know that you only have .c and .cpp files in SOURCES (or want to support other extensions in SOURCES being converted to .o) you could use $(basename) and $(addsuffix):

    OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))