makefile efficiency

Programming, for all ages and all languages.
Post Reply
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

makefile efficiency

Post 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!
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: makefile efficiency

Post 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?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

You don't need to define the lex.o and myProg.o rules - make has default *.c files built in.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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"...
Every good solution is obvious once you've found it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

What solar said (hint, there is.)

Also make has builtin rules for flex and bison.
Zekrazey1
Member
Member
Posts: 37
Joined: Sat Mar 10, 2007 8:28 am

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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.
Zekrazey1
Member
Member
Posts: 37
Joined: Sat Mar 10, 2007 8:28 am

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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
Last edited by JamesM on Wed Sep 12, 2007 8:46 am, edited 1 time in total.
Zekrazey1
Member
Member
Posts: 37
Joined: Sat Mar 10, 2007 8:28 am

Post by Zekrazey1 »

Ok.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

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