[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Creating makefile



On Sat, 11 Dec 2004 13:19:45 +0800, Wilson Woon <thwoon2002 gmail com> wrote:
> Hi all...
> 
> I have problem creating Makefile to compile my C program files. I have
> two .c and one .h files. Here's my Makefile..
> 
> # use "gcc" to compile source files.
> CC = gcc
> # the linker is also "gcc". It might be something else with other compilers.
> LD = gcc
> # Compiler flags go here.
> CFLAGS = -g -Wall
> # Linker flags go here. Currently there aren't any, but if we'll switch to
> # code optimization, we might add "-s" here to strip debug info and symbols.
> LDFLAGS =
> # use this command to erase files.
> RM = /bin/rm -f
> # list of generated object files.
> OBJS = main.o action.o
> # program executable file name.
> PROG = result
> 
> # top-level rule, to compile everything.
> all: $(PROG)
> 
> # rule to link the program
> $(PROG): $(OBJS) \
>         $(LD) $(OBJS) -o $(PROG)
> 
> # now comes a meta-rule for compiling any "C" source file.
> %.o: %.c \
>         $(CC) $(CFLAGS) -c $<
> 
> # rule for cleaning re-compilable files.
> clean: \
>         $(RM) $(PROG) $(OBJS)
> 
> The main.c and action.c files compiled successfully. However the
> result execution file failed with make: *** No rule to make target
> `gcc', needed by `main.o'.  Stop. error message.
> 
> Can anyone help me?
> 
> Thank you

I think the three "\" are giving you problems.  This is used at the
end of a line to continue it on the next line, so it is as if the next
line is on the same line.  The syntax should be
rule: dependencies
          command1
          command2
          .....

Putting the "\" makes the first command part of the dependency lines. 
Also, for the rule for compiling .c files, you can also use:
.c.o:
instead of
%.o: %.c

As Marvin suggested, you might want to not use as many variables.  For
compiling a two source file program, all of this is a little
excessive.  Though it is more general if you want to follow the same
form for bigger, more complex projects.  For instance, you could use
the following for the Makefile:
CC= gcc -g -Wall

all: result

result: main.o action.o
<tab>$(CC) main.o action.o -o result

.c.o:
<tab>$(CC) -c $<

clean:
<tab>/bin/rm -f main.o action.o result

Of course, replace the "<tab>" with an actual tab.  I can't get gmail
to place a tab in the email.  But, removing the "\"s in your first one
should work.

Jonathan


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]