Udara S.S Liyanage
Udara S.S Liyanage

Reputation: 6453

Makefile error: No rule to make target

SOURCES = server.c

TARGET = Server

CC = gcc

all: $(SOURCES) $(TARGET) 


$(CC) $(SOURCES) -o $(TARGET) 

clean:


rm -rf $(TARGET) 

Above is the Makefile of my web server. Though server.c file is in the directory this gives the fallowing error

make: *** No rule to make target `Server', needed by `all'.  Stop.

What is the mistake I've made and how to solve it.

Upvotes: 12

Views: 66473

Answers (3)

A Star
A Star

Reputation: 637

My issues was I had the name and the command on the same line. Make: sure you are using tabs and not spaces. (no pun intended)

BEFORE (Broken)

build: docker build...

AFTER

build:
        docker build...

Upvotes: 0

piotr wozniak
piotr wozniak

Reputation: 21

just do "make clean" to clean all links, then run make again. Everything should be good.

Upvotes: 2

Beta
Beta

Reputation: 99164

I think your makefile got garbled somewhere between your machine and the post, but there is a simple fix that I think will work:

all: $(SOURCES)

That will (probably) solve the problem and make the error go away-- if that's all you want then you can stop reading. But there are still things wrong with this makefile, so we can make some more improvements.

First, a little adjustment to make it match what I think your makefile really says:

SOURCES = server.c

TARGET = Server

CC = gcc

all: $(SOURCES) $(TARGET)
    $(CC) $(SOURCES) -o $(TARGET) 

clean:
    rm -rf $(TARGET) 

The first three lines and the clean rule are all right, we'll ignore those. Now we give TARGET its own rule and straighten out the prerequisites:

all: $(TARGET)

$(TARGET): $(SOURCES)
    $(CC) $(SOURCES) -o $(TARGET) 

Now we make all PHONY (since it doesn't really make a file called "all"), and introduce automatic variables to make the TARGET rule more robust and less redundant:

.PHONY: all
all: $(TARGET)

$(TARGET): $(SOURCES)
    $(CC) $< -o $@ 

There's more to learn if your codebase gets more complicated, but that'll do for now.

Upvotes: 17

Related Questions