1

I am trying to compile a static library of the sorts:

foo.c
foo.h

My makefile looks like:

foo.o: foo.c
   cc -c foo.c
libfoo.a: foo.o
   ar -rv libfoo.a foo.o

I just call make from the directory.

It compiles the .c file and gives a .o file, however, I don't get a .a file. Although, I can run the exact commands sequentially and get the .a file.

I'm sure it's something simple I'm messing up. I've looked all over but the examples aren't really helping as they're much more intricate than my circumstance.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Baker
  • 43
  • 10
  • How exactly do you built? Maybe show the commandline which calls make. – Yunnosch Mar 08 '20 at 21:47
  • @Yunnosch That above is my makefile. In the command line I tried doing the individual commands `cc -c foo.c` and then `ar-rv libfoo.a foo.o` and it worked. I'm trying to find out why it doesn't work in the makefile though. – Baker Mar 08 '20 at 21:51
  • 1
    How do you call make? `make`? `make mehappy`? `make foo`? How? Or to ask differently, how do you use the makefile you have shown? – Yunnosch Mar 08 '20 at 21:52
  • @Yunnosch I just call `make` from the directory. – Baker Mar 08 '20 at 21:56
  • Try `make libfoo.a`. Also try inserting echo commands `echo compiling foo.c` and `echo creating lib`. Just to prove that your way of calling make actually triggers something. – Yunnosch Mar 08 '20 at 21:58
  • @Yunnosch I have to be able to compile just using `make`. I put echo statements in both the .o and .a sections. Only the .o prints, so it isn't even getting into the section to make the .a file. – Baker Mar 08 '20 at 22:04
  • @kaylum This actually did answer my question. I needed to add a target `.DEFAULT_GOAL := mytarget` at the top of the makefile to ensure that it was compiled completely. – Baker Mar 08 '20 at 22:11

1 Answers1

2

Just calling make without parameters will by default use the first goal, which is foo.o. Since that does not depend on libfoo.a (and why should it of course), the second recipe is never triggered.
The result is a .o file but no .a file.

From https://www.gnu.org/software/make/manual/html_node/Goals.html

By default, the goal is the first target in the makefile

If for any reason you are forced to use make as just make, then reorder the parts in your makefile, to ensure that the first target is the one you need.

Alernatively, as described on the same page a few lines later

You can manage the selection of the default goal from within your makefile using the .DEFAULT_GOAL variable (see Other Special Variables).

Yunnosch
  • 26,130
  • 9
  • 42
  • 54