Mr Deepak Gupta
Mr Deepak Gupta

Reputation: 1

makefile script is showing error while checking particular directory is present or not

Below makefile is for checking if the 'obj' directory exist or not.

DIR_TO_CHECK_FOR := '/home/deepak/makefilefolder/obj'

objects= obj/file1.o obj/file2.o obj/file3.o

DIR_TO_CHECK_FOR := '/home/deepak/makefilefolder/obj'

objects= obj/file1.o obj/file2.o obj/file3.o

all: create

create: ifeq ("$(wildcard $(DIR_TO_CHECK_FOR))", "") @echo "Folder does not exist" else @echo "Folder exists" endif

$(objects): obj/%.o: src/%.c gcc -c $< -o $@

clean: rm -rf obj/

This is showing error while running 'make create': ifeq ("", "") /bin/sh: -c: line 1: syntax error near unexpected token "",' /bin/sh: -c: line 1: ifeq ("", "")' make: *** [makefile:8: create] Error 2

I have tried many things please help me out to resolve this.

i have tried to correct the directory path. checked the wildcard syntax of create target.

Upvotes: 0

Views: 59

Answers (2)

Armali
Armali

Reputation: 19375

The ifeq is a conditional expression of make, not a shell command, so one may not tab-indent it; unindent the ifeq, else and endif, and the error is gone.

Upvotes: 0

Verpous
Verpous

Reputation: 746

Makefiles don't allow if statements inside rules like that, and commands are supposed to be indented one line after the target definition. A makefile target is generally of the form:

target: dependency1 dependency2...
    commands...

What you can do is change create to be like this:

create:
    [[ -d "$(DIR_TO_CHECK_FOR)" ]] && echo "Folder exists" || echo "Folder does not exist"

Note what I did here: I used bash constructs to check if the directory exists instead of makefile constructs.

Assuming that in the end you want to create the directory if it doesn't exist, this is still the wrong way to go about it. This is exactly what makefiles are made for, all you need to do is drop the create target altogether and instead have:

$(DIR_TO_CHECK_FOR):
    mkdir -p $(DIR_TO_CHECK_FOR)

And then in the all target you should do:

all: | $(DIRECTORY_TO_CHECK_FOR)

The | tells make to not care if the directory has been changed, and only run the rule if it doesn't exist at all.

Upvotes: 0

Related Questions