0

Noob question. I want to compile bar.c with object file foo.o to an executable bar. I have this in a Makefile:

bar: foo.o 
    cc bar.c foo.o -o bar

I run $ make -n and get :

cc    -c -o foo.o foo.c 
cc bar.c foo.o -o bar

I'm looking at the first line of output cc -c -o foo.o foo.c. I didn't write an explicit rule compiling foo.c to foo.o. Does make do this implicitly when it sees a .o target?

1
  • 3
    Indeed. See implicit rules, under "Compiling C programs". Commented Apr 18, 2022 at 6:30

1 Answer 1

3

Yes, GNU make has a catalog of built-in rules:

Compiling C programs
n.o is made automatically from n.c with a recipe of the form ‘$(CC) $(CPPFLAGS) $(CFLAGS) -c’.

Linking a single object file
n is made automatically from n.o by running the linker (usually called ld) via the C compiler. The precise recipe used is ‘$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)’.

so you could just write:

bar: bar.o foo.o
Sign up to request clarification or add additional context in comments.

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.