Michael Schmidt
Michael Schmidt

Reputation: 415

Why wouldn't make not execute a simple line in the makefile

This is my first time using make, and i've been spinning my wheels trying to get past an issue. I can't understand why a simple echo never gets executed:

CFLAGS = -Wall 
LDFLAGS = -g
CC = gcc
SRCS = p4a.c p4b.c

p4static:   p4a.c
    gcc $(LDFLAGS) -o $@ $< -static -L. -lpthread


p4dynlink:  p4a.c
    @echo "this doesn't print/echo/execute"
    gcc $(LDFLAGS) -o p4dynlink $< -L. -lpthread

I'm using tab instead of spaces. Here is the outputs:

mike@elementary:~/p4$ make
gcc -g -o p4static p4a.c -static -L. -lpthread
mike@elementary:~/p4$ make
make: `p4static' is up to date.

Upvotes: 0

Views: 43

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80961

From How make Processes a Makefile:

By default, make starts with the first target (not targets whose names start with ‘.’). This is called the default goal. [....]

Thus, when you give the command:

make

make reads the makefile in the current directory and begins by processing the first rule.

So when you type make make tries to build your p4static target which doesn't have an echo line. And the next time you run make it says that target is up to date and has nothing to do.

To build p4dynlink you need to tell make to build that target make p4dynlink.

You can set the default goal manually (in the makefile) with .DEFAULT_GOAL:

# Set our own.
.DEFAULT_GOAL := foo

But convention is usually to create an all target as the first target and have it "Do the Right Thing" by default.

So in your case, assuming you wanted both targets built by default, you would use:

all: p4static p4dynlink

Upvotes: 1

Related Questions