5

Here is my project:

project
  |--- main.cpp
  |--- makefile
  |--- test
        |--- Test.cpp
        |--- Test.h

Here is the makefile:

g++1x:=g++ -std=c++14 -stdlib=libc++ -MMD -MP
cflags:= -Wall -lncurses

PATHS:=./ ./test/
TARGET:=matrix.out
SRC:=$(foreach PATH,$(PATHS),$(wildcard $(PATH)/*.cpp))
OBJDIR:=.obj
OBJ:=$(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.o)))


.PHONY: install
install: $(OBJDIR) $(TARGET)

$(OBJDIR):
    mkdir -p $(OBJDIR)

$(TARGET): $(OBJ)
    $(g++1x) $(cflags) -o $@ $^ -g


$(OBJDIR)/%.o: %.cpp
    $(g++1x) -c -o $@ $< -g

$(OBJDIR)/%.o: ./test/%.cpp
    $(g++1x) -c -o $@ $< -g

-include $(addprefix $(OBJDIR)/,$(notdir $(SRC:.cpp=.d)))

.PHONY: clean
clean:
    rm -f $(TARGET)
    rm -rf $(OBJDIR)

It works well but I have two questions:
1) Is it possible to avoid foreach for PATHS so that I can use the same makefile for all of cpp projects?
2) As you see, to generate main.o and Test.o I write two blocks:
$(OBJDIR)/%.o: ./test/%.cpp and $(OBJDIR)/%.o: %.cpp.
Is it possible to write only once?
I've tried as below but it doesn't work:

$(OBJDIR)/%.o: $(foreach PATH,$(PATHS),$(wildcard $(PATH)/%.cpp))
        $(g++1x) -c -o $@ $< -g

I've even tried like this but it doesn't work still:

$(OBJDIR)/%.o: %.cpp ./test/%.cpp
    $(g++1x) -c -o $@ $< -g
2
  • Note that your use of an "OBJDIR" is rather ... clumsy. You might enjoy reading How Not to use VPATH and Multi-Architecture Builds, were the framework provided in the later link can also be used to perform an out-of-source build (which seems to be what you want). Apr 4, 2017 at 23:39
  • Have a look at my project github.com/cppfw/prorab it solves many problems already, so no need to reinvent own makefile template.
    – igagis
    Oct 4, 2021 at 22:06

2 Answers 2

12

You should keep the source tree into your object tree. This way it will be easier to create global rules and keep dependencies.

# Use the shell find command to get the source tree
SOURCES := $(shell find * -type f -name "*.c")

OBJDIR  := .objects

# Keep the source tree into the objects tree
OBJECTS := $(addprefix $(OBJDIR)/,$(SOURCES:.c=.o))

all: mytarget

mytarget: $(OBJECTS)
    $(CC) $^ -o $@

# As we keep the source tree we have to create the
# needed directories for every object
$(OBJECTS): $(OBJDIR)/%.o: %.c
    mkdir -p $(@D)
    $(CC) -MMD -MP -c $< -o $@

-include $(OBJECTS:.o=.d)

$ make
mkdir -p .objects
cc -MMD -MP -c main.c -o .objects/main.o
mkdir -p .objects/test
cc -MMD -MP -c test/test.c -o .objects/test/test.o
cc .objects/main.o .objects/test/test.o -o mytarget

$ tree -a
.
├── main.c
├── Makefile
├── mytarget
├── .objects
│   ├── main.d
│   ├── main.o
│   └── test
│       ├── test.d
│       └── test.o
└── test
    ├── test.c
    └── test.h

3 directories, 9 files

EDIT: You can reduce the number of mkdir to the minimum by adding the object directory as an order only prerequisites:

# Do not create the directory
$(OBJECTS): $(OBJDIR)/%.o: %.c
    $(CC) -MMD -MP -c $< -o $@

# Every target finishing with "/" is a directory
$(OBJDIR)%/:
    mkdir -p $@

# Add the "directory/" as an order only prerequisite
$(foreach OBJECT,$(OBJECTS),$(eval $(OBJECT): | $(dir $(OBJECT))))
3
  • Wonderful! This is exactly what I need.
    – Yves
    Apr 5, 2017 at 12:10
  • Just a little tiny question: mkdir -p .objects will be executed many times, it seems that it is executed for each cpp file. Is it possible to avoid this?
    – Yves
    Apr 5, 2017 at 12:45
  • @Yuhui I found a way which decrease the mkdir number. It's a little hack with a foreach because you cannot simply add it to the first line, but it is pretty efficient. Apr 5, 2017 at 13:31
0

Because I can't edit jmlemetayer's answer, here's how to extend the commands for projects with both C and CPP source:

Firstly to generate the object list, just apply the patsubst twice, once for each extension:

SRC_LST := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES)))

OBJECTS := $(addprefix $(OBJDIR)/,$(SRC_LST))

Then to handle both .cpp and .c files in the implicit rule for compiling:

.SECONDEXPANSION:
$(OBJECTS): $(OBJDIR)/%.o: $$(wildcard %.c*)
    mkdir -p $(@D)
ifeq "$(suffix $<)" ".cpp"
    $(CPP) -MMD -MP -c $< -o $@
else
    $(CC) -MMD -MP -c $< -o $@
endif

If there is a better way I'm happy to update this.

2
  • Why couldn't you just add a separate rule for .cpp files? Then you wouldn't need to rely on SECONDEXPANSION and $(suffix ...) trickery.
    – Phil Hord
    Jan 5, 2022 at 20:32
  • 1
    @philhord because I tried that and ran into a multitude of errors. At least this works.
    – fret
    Jan 6, 2022 at 7:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.