0

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.

1
  • Please edit your question and format it properly by making your included code into a code block. Else it's not readable. Commented May 16, 2023 at 14:42

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You should not use [[ in makefiles. That is a bash construct which is not present in POSIX standard shell and so if you use it your makefile will not work on any system where the default shell is not bash (unless you modify your makefile to specifically request bash, such as adding SHELL := /bin/bash).
Thanks alot all for your answers, it is working fine now.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.