Search This Blog

Wednesday, February 22, 2012

HowTo: check if a file exists in a makefile (using makefile conditionals)

How to check if a file exists in a makefile - Stack Overflow

There are so many examples with redundant uses of wildcard
$(findstring ., $(wildcard *.))

Use the "wildcard" function:

$(wildcard *.h) 
EDIT: in order to match a specific list, do
$(wildcard $(HEADER_FILES)) 
There is no need to use $(filter ...), the wildcard function automatically filters files which don't exist.

ifeq ($(wildcard $(HEADER_FILES)), )
# This will stop the make trying to resolve HEADER_FILES for echo :)
echo "ERROR: $(HEADER_FILES)"
else
# File exists, continue or do something else...
endif

1 comment:

  1. I was trying to find a way to check if a file exists in makefile and this post helped me.

    Thanks.

    ReplyDelete