Sample Header Ad - 728x90

Why does make's `dir` function add additional directories?

7 votes
1 answer
535 views
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