Page 1 of 1

makefile efficiency

Posted: Tue Sep 11, 2007 2:16 pm
by eboyd
I have the following makefile:

Code: Select all

myProg: lex.o myProg.o
    gcc -g lex.o myProg.o
    mv a.out myProg

lex.o: lex.c
    gcc -g -c lex.c

myProg.o: myProg.c
    gcc -g -c myProg.c

lex.c: myProg.c
    flex cppscanner.l
The make file will build 'myProg' but my teacher says that my makefile is doing too much work if only myProg.c is changed. I don't want anyone to give me the answer (as obvious as it may be to some of you) but point me in the right direction if possible.

Thanx All!

Re: makefile efficiency

Posted: Tue Sep 11, 2007 2:26 pm
by Candy
eboyd wrote:I have the following makefile:

Code: Select all

myProg: lex.o myProg.o
    gcc -g lex.o myProg.o
    mv a.out myProg

lex.o: lex.c
    gcc -g -c lex.c

myProg.o: myProg.c
    gcc -g -c myProg.c

lex.c: myProg.c
    flex cppscanner.l
The make file will build 'myProg' but my teacher says that my makefile is doing too much work if only myProg.c is changed. I don't want anyone to give me the answer (as obvious as it may be to some of you) but point me in the right direction if possible.

Thanx All!
What is the relation between lex.c and myProg.c?

Posted: Tue Sep 11, 2007 3:15 pm
by JamesM
You don't need to define the lex.o and myProg.o rules - make has default *.c files built in.

Posted: Tue Sep 11, 2007 6:56 pm
by eboyd
lex.c is made by scanning myProg.c using a program called "Flex".

The rule for lex.c uses the command "flex cppscanner.l" to lexically scan myProg.c and convert it to html.

Posted: Tue Sep 11, 2007 7:10 pm
by eboyd
You don't need to define the lex.o and myProg.o rules - make has default *.c files built in.
I got rid of the rules for lex.o and myProg.o. It works, but I still get the message from my teacher (this is an automated assignment):

Code: Select all

$ ~/bin/makeAsst.pl
"Your makefile does too much work when only myProg.c has been changed."
Here is the output using make -n:

Code: Select all

$ make -n
flex cppscanner.l
cc   -c -o lex.o lex.c
cc   -c -o myProg.o myProg.c
gcc -g lex.o myProg.o
mv a.out myProg
I'm getting seriously frustrated w/this assignment!

Posted: Wed Sep 12, 2007 12:25 am
by Solar
I would check if there's some option to GCC that allows you to specify the name of the output file, to get rid of the additional "mv"...

Posted: Wed Sep 12, 2007 4:38 am
by JamesM
What solar said (hint, there is.)

Also make has builtin rules for flex and bison.

Posted: Wed Sep 12, 2007 6:02 am
by Zekrazey1
You said that "flex cppscaner.l" scans myProg.c and converts it to html, but this:

Code: Select all

lex.c: myProg.c
    flex cppscanner.l
indicates that you think a c source file is generated from that command.

Posted: Wed Sep 12, 2007 7:26 am
by JamesM
Zekrazey1: You should google flex - It's a lexical scanner generator. It takes a scanner definition (in a file with extension .l) and generates a C file which implements that scanner. So the rule *should* output a .c file.

Posted: Wed Sep 12, 2007 7:37 am
by eboyd
You were right Zekrazey1. I was reading the assignment wrong and misunderstood. Here's what fixed it:

Code: Select all

lex.c: cppscanner.l
    flex cppscanner.l
Thanx everyone anyway. I can't say I haven't learned something about makefiles.

Posted: Wed Sep 12, 2007 7:55 am
by Zekrazey1
I was merely following Candy's line of questioning ;).
Zekrazey1: You should google flex - It's a lexical scanner generator. It takes a scanner definition (in a file with extension .l) and generates a C file which implements that scanner. So the rule *should* output a .c file.
The point was that there was a difference between what eboyd said (in response to Candy) and what his makefile said about the output of flex.

Posted: Wed Sep 12, 2007 8:17 am
by JamesM
lex.c is made by scanning myProg.c using a program called "Flex".p
This is correct.

Code: Select all

lex.c: myProg.c 
    flex cppscanner.l
This is also correct, apart from the dependency. lex.c, a C file, is created by running 'flex' on cppscanner.l.

I don't see what this difference is you're talking about...?

JamesM

Posted: Wed Sep 12, 2007 8:26 am
by Zekrazey1
Ok.

Posted: Wed Sep 12, 2007 10:24 am
by eboyd
What is the relation between lex.c and myProg.c?
Oh, you were right too Candy. I just didn't realize where you were going with your question until after the fact. Thanx.

Posted: Wed Sep 12, 2007 10:30 am
by Candy
Zekrazey1 wrote:I was merely following Candy's line of questioning ;).
He said he didn't want me to spoil the answer so I was a bit more cryptic than I usually am... I thought you would figure out that they should have no relation since you don't pass the name of the file in the line just below it.

On the other hand, this really does teach you something since you know what a basic makefile looks like and you can craft a makefile for a diminutive project. That's fair for school assignments.