开发者

Problem with makefile

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 .开发者_如何学C.. ?

 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 ...


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.


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.


"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).


In addition to indentation problems, you should drop -c from CFLAGS, else the produced hello will not be an executable but a .o file without the correct name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜