Search code examples
makefilegnu-make

Makefile: Include gives "No such file or directory" although file exists


I am trying to include another Makefile ("Makefile2") in the same folder into a Makefile like this:

OTHER_MAKEFILE_PATH = ./Makefile2

.PHONY: build
build:
    @test -e $(abspath $(OTHER_MAKEFILE_PATH)) && echo "File exists" || echo "File does not exists"
    -include $(abspath ./Makefile2)

The console output is

❯ make build
File exists
include /Users/xxx/yyy/test/Makefile2
make: include: No such file or directory
make: [build] Error 1 (ignored)

I don't get why the include gives a file-not-found error? Anybody got an idea?

I am using GNU Make 3.81 on macOS 14.5 with M2 chip.

I tried it with absolute and relative paths, neither worked. I expect that when the file exists the other makefile is included (at least not giving an error if there is nothing to do for it).


Solution

  • As HolyBlackCat figured out, it's because the include directive needs to be outside of the recipe section.