Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Makefiles do not have recursive build subdirectories

I have the following directory structure. I try to compile all files into object files.

├── Makefile
├── prime_probe.c
└── utils
    ├── Makefile
    ├── caches_info.c
    ├── caches_info.h
    ├── caches_util.c
    ├── caches_util.h
    ├── configure.h
    ├── list_struct.h
    ├── list_utils.c
    └── list_utils.h

Makefile

THIS_DIR  := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
OBJDIR = obj

CFLAGS = -I./$(OBJDIR) -g -fPIC -std=gnu99

UTILS_DIR := utils
OBJS_UTL   = $(addprefix $(UTILS_DIR)/$(OBJDIR)/,$(notdir $(patsubst %.c,%.o,$(shell find $(UTILS_DIR)/*.c))))
SRCS_PRIME =                  \
    prime_probe.c       

OBJS_PRIME = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(SRCS_PRIME)))
all: clean utils $(OBJS_PRIME) 
$(OBJDIR)/%.o: %.c | objdir
    $(CC) $(CFLAGS) -c $< -o $(OBJDIR)/$(notdir $@)


utils:
    $(MAKE) -C $(UTILS_DIR) 

objdir:
    @mkdir -p $(OBJDIR)

clean:
    rm -rf $(OBJDIR)
    $(MAKE) clean -C $(UTILS_DIR)/

The subdirectory Makefile is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

THIS_DIR  := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
##################
# Build directory
##################
OBJDIR = obj

CFLAGS = -I./$(OBJDIR) -g -fPIC -std=gnu99 -static
####################
# Files and folders
####################

SRCS_PRIME = $(shell find ./*.c)

OBJS_PRIME = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(SRCS_PRIME)))

##########
# Targets
##########

all:  $(OBJS_PRIME)
$(OBJDIR)/%.o: %.c | objdir
    $(CC) $(CFLAGS) -c $< -g -o $@

objdir:
    @mkdir -p $(OBJDIR)

clean:
    rm -rf $(OBJDIR)

When I execute the make command, the files in the subdirectory are not compiled.
I do make utils alone and this shows the subdirectories are up to date. But when I enter the subdirectory and execute make, I can see that the file will be compiled.

Why is this so? thanks!!!

root@096b64b8fd50:/usr/local/src/gem5/programs/covert_channel# make
rm -rf obj
make clean -C utils/
make[1]: Entering directory '/usr/local/src/gem5/programs/covert_channel/utils'
rm -rf obj
make[1]: Leaving directory '/usr/local/src/gem5/programs/covert_channel/utils'
cc -I./obj -g -fPIC -std=gnu99 -c prime_probe.c -o obj/prime_probe.o

root@096b64b8fd50:/usr/local/src/gem5/programs/covert_channel# make utils
make: 'utils' is up to date.

root@096b64b8fd50:/usr/local/src/gem5/programs/covert_channel# cd utils/
root@096b64b8fd50:/usr/local/src/gem5/programs/covert_channel/utils# make
cc -I./obj -g -fPIC -std=gnu99 -static -c caches_info.c -g -o obj/./caches_info.o
cc -I./obj -g -fPIC -std=gnu99 -static -c caches_util.c -g -o obj/./caches_util.o
cc -I./obj -g -fPIC -std=gnu99 -static -c list_utils.c -g -o obj/./list_utils.o

>Solution :

If you want build utils as a non-file target then you need to specify it as such with:

.PHONY: utils

I suggest don’t use recursive make, besides not providing make with a global state, it also makes it unnecessarily complicated (IMHO).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading