I'm learning C Programming through "Learn C the Hard Way." I am currently (for quite a long time) on Exercise 2, which can be found here: http://c.learncodethehardway.org/book/ex2.html
In the extra credit section, there is a challenge which asks us to edit the Makefile so we can compile the .c file (ex1.c) using just the command "make". This is what the challenge exactly says:
Create an all: ex1 target that will build ex1 with just the command make.
Through looking here and a bunch of other places, this is the code I think is correct:
CFLAGS="-Wall" -g
all: ex1
CFLAGS="-Wall" -g
clean:
rm -f ex1
However, when I try to run this, I get the compiled file and keep getting an error:
cc "-Wall" -g ex1.c -o ex1
CFLAGS="-Wall" -g
/bin/sh: 1: -g: not found
make: *** [all] Error 127
What is happening here? What is the reason behind the error message? How can I fix this?
Thanks guys.
If all
is the first target in your makefile (I'm making a wild guess here since you've not showed the entire Makefile), make would choose that as the first target to build, when you do not pass an explicit target from the command line (i.e. mere make
)
ex1
is a pre-requisite for all
, so it builds it first and hence you see the following line:
cc "-Wall" -g ex1.c -o ex1
After ex1
is built, make tries to execute the recipe for all, where it executes the following command:
CFLAGS="-Wall" -g
Since -g
by itself is not a valid command or application that can be executed in the shell, you get this error:
/bin/sh: 1: -g: not found
Solution:
You've already defined CFLAGS
in the beginning of the makefile, so no need to define it once again in the recipe. Even if you wish to override the variable CFLAGS
, you should not be using it directly as part of the recipe.
Define all
and clean
as PHONY
targets, if you've not already done so. The target all
and clean
do not create a file named all
or clean
after the recipe is executed, so it is a good practice to classify these targets as PHONY
.
You don't need double quotes around -Wall
The following Makefile has all these corrections:
CFLAGS = -Wall -g
all: ex1
clean:
rm -f ex1
.PHONY: all clean
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With