tags:

views:

60

answers:

3

hiii , i just started studying makefiles and wrote the following one for a simple hello.c file. it shows some error saying :

makefile1:5: * missing separator. Stop.

What is the wrong here ... ?

 CC=gcc
    CFLAGS=-c -Wall

    hello: hello.c
    $(CC) $(CFLAGS) hello.c -o hello

    clean:
     rm -rf *.o

And , Is it always a better option to use a makefile or there are any specific cases to not use them ...?

Please correct me if i am wrong anywhere ...

+2  A: 

Make sure you are not missing any tabs before you write the rule to generate a target:

CC=gcc
CFLAGS=-c -Wall

hello: hello.c
<TAB>$(CC) $(CFLAGS) hello.c -o hello

.PHONY: clean
clean:
<TAB>rm -rf *.o

Its good to write the .PHONY. You can find the reason here.

codaddict
Now it says ... " NO rule to make target hello.c , needed by hello ".... what does that mean ?
ravi
Looks like there is no file named `hello.c` in the directory.
codaddict
It compiled.. It showed me "$(CC) $(CFLAGS) hello.c -o hello" ...but when i try to compile suing ./hello it doesnt....Any idea why the prblem is ...?
ravi
`./hello` does not compile..its runs. What do you get when you do `./hello`?
codaddict
I get bash: /hello:permission denied... I meant to say "run" sry for the worng term.. This occurs even when i am the super user...
ravi
+1  A: 

First, your targets should not be indented. Second, make sure you're using tab characters not spaces to indent.

CC=gcc
CFLAGS=-c -Wall
.PHONY: clean # There's no clean file.
hello: hello.c
    $(CC) $(CFLAGS) hello.c -o hello
clean:
    rm -rf *.o

As to your other question, makefiles are used everywhere. Whether you like them or not, learning how to maintain them is a good idea. Personally, I like how magic they are. They can be great time savers. They can also be horrendous time sinks if you find yourself having to debug complex ones.

Nathon
A: 

"Missing separator" means you probably didn't use a tab character in your CC or rm lines. Try reformatting the file as follows

CC=gcc
CFLAGS=-c -Wall

hello: hello.c
<TAB>$(CC) $(CFLAGS) hello.c -o hello

clean:
<TAB>rm -rf *.o

Make is picky in that all command lines must lead with a tab character. Not 4 spaces, not 8 spaces, but an actual tab (ASCII 0x09).

John Bode