antigravityguy antigravityguy - 2 months ago 18
C Question

Using "make" C Programming (Learn C the Hard Way)




I'm learning C Programming through "Learn C the Hard Way." I have am currently on Exercise 1, which can be found here: http://c.learncodethehardway.org/book/ex1.html

I understand the concept being covered, but don't understand the compiling process. While using the "make" command in the command line, why does this work:

$ make ex1


And this not work:

$ make ex1.c


I was actually just running the second command until a minute ago. I eventually figured it out though. Until I did, I kept getting this error message:

make: nothing to be done for 'ex1.c'


While this is a just a technicality, I would still like to know what's happening. Thanks :)

Answer

Both work; they just do different tasks.

  • make ex1.c says "using the rules in the makefile plus built-in knowledge, ensure that the file ex1.c is up to date".

    The make program finds ex1.c and has no rules to build it from something else (no RCS or SCCS source code, for example), so it report 'Nothing to be done'.

  • make ex1 says "using the rules in the makefile plus built-in knowledge, ensure that the file ex1 is up to date".

    The make program finds that it knows a way to create an executable ex1 from the source file ex1.c, so it ensures that the source file ex1.c is up to date (it exists, so it is up to date), and then makes sure the ex1 is up to date too (which means 'file ex1 was modified more recently than ex1.c). If you've edited ex1.c since you last ran make, it will do the compilation to make ex1 newer than ex1.c. If the compilation fails, the file ex1 won't exist, so you'll get another recompilation next time too. And once it is up to date, then make won't rebuild ex1 again until you modify the source again.

This is the way make is designed to work.