gcc - Makefile: recipe for target 'all' failed after adding dependency rule

Viewed 8682

I have a Makefile that fails every time I run it. I have been altering a Makefile that is compiling a bunch of files into a directory that needs to be made if it is not already. all: fails, and I honestly have no clue why it does.

Here is what my Makefile looks like:

CC=gcc
SRCDIR=src
INCDIR=include
BINDIR=../bin
CFLAGS = -flags
LIBS = -llibrary1 -llibrary2

all: 
    $(BINDIR) \
    $(BINDIR)/program1 \
    $(BINDIR)/program2 \
    $(BINDIR)/program3

$(BINDIR)/program1: $(SRCDIR)/program1.c | $(BINDIR)
    $(CC) $(CFLAGS) $< -o $@ $(LIBS)

$(BINDIR)/program2: $(SRCDIR)/program2.c | $(BINDIR)
    $(CC) $(CFLAGS) $< -o $@ $(LIBS)

$(BINDIR)/program3: $(SRCDIR)/program3.c | $(BINDIR)
    $(CC) $(CFLAGS) $< -o $@ $(LIBS)

$(BINDIR):
    mkdir -p $@

clean:
    rm -f $(BINDIR)/*

I know that the issue has to do with the $(BINDIR), but I can't figure out what to do. The last question I asked on StackOverflow gave me some resources (such as GNU's documentation here). From my understanding, $(BINDIR) should be made first when I run make, and then the other rules should execute without any issues.

Here is the error I receive, which I know means that $(BINDIR) is not being created before the rest of the rules execute.

user@user-Notebook:~/project/folder$ make
../bin \
    ../bin/program1 \
    ../bin/program2 \
    ../bin/program3
make: ../bin: Command not found
Makefile:16: recipe for target 'all' failed
make: *** [all] Error 127

What am I missing?

2 Answers

Posting this as an answer as it wouldn't fit in a comment:

Would make more sense that $(BINDIR)(/program), to be dependent(s) for the all target, not commands:

all: $(BINDIR) $(BINDIR)/program1 $(BINDIR)/program2 $(BINDIR)/program3

Check [GNU]: An Introduction to Makefiles for more details.

Things could be taken even further (e.g. writing an pattern rule that would handle all the files), in order to make the Makefile more compact.

@EDIT0:

Per @o11c's comment, I removed some part of the answer that was incorrect

As currently written your 'all' rule has no dependencies and a non empty recipe. Thus make is trying to execute your dependency list as shell instructions. This is why you see the command not found error, it is from the shell. Remove the newline after all: and it should work.

Related