Sierra Jo Sallee
Sierra Jo Sallee

Reputation: 1

Why is my simple makefile only printing the rule?

My makefile is written as follows:

all: hello

hello: hello.c
    gcc hello.c -o hello

When I type 'make' into the terminal, the rule gcc hello.c -o hello is printed, but not run. I can tell because no .o files are being created. If I type in the rule to the terminal, it works as expected. I'm using vim to edit the file. What's wrong?

Upvotes: 0

Views: 85

Answers (2)

Miguel Carvalho
Miguel Carvalho

Reputation: 416

To generate object files, use -c flag.

gcc -c hello.c

For example,

all: hello

hello: hello.o
    gcc -o hello hello.o

hello.o: hello.c
    gcc -c hello.c

Upvotes: 1

larsks
larsks

Reputation: 311606

The command gcc hello.c -o hello will not generate any .o files. It will only generate the output file you've requested, hello.

You would normally write your Makefile more like this:

all: hello

hello: hello.o
    $(CC) -o $@ $<

That is, the executable depends on the object files (.o), and make's built-in rules know how to generate .o files from .c files.

Upvotes: 1

Related Questions