When I apply
[
dir
](https://www.gnu.org/software/make/manual/make.html#index-dir)
to a list of files, it adds additional directories. For example, with
the following file/directory structure:
.
├── a
│ └── foo.c
├── b
│ └── foo.c
├── c
│ └── foo.c
├── d
├── Makefile
└── readme.md
and this Makefile
:
FILES=*/foo.c
DIRS=$(dir */foo.c)
all:
@echo $(FILES)
@echo $(dir $(FILES))
@echo $(DIRS)
running make
results in the following output:
a/foo.c b/foo.c c/foo.c
a/ b/ c/ d/
a/ b/ c/ d/
Why is the directory d
added to the list? The
[definition](https://www.gnu.org/software/make/manual/make.html#index-dir)
of $(dir names)
says
> Extracts the directory-part of each file name in names.
Thus, I expect $(dir $(FILES))
or $(dir */foo.c)
to be only a/ b/ c/
, that is, the
directories where the foo.c
files reside in.
I am using GNU Make 4.4.1.
Asked by Robert
(215 rep)
Sep 18, 2025, 05:33 AM
Last activity: Sep 19, 2025, 08:56 PM
Last activity: Sep 19, 2025, 08:56 PM