Create Makefile from source files

  1. The problem statement, all variables and given/known data:

Create a makefile for a set of source files. Several sources files are given but we are not allowed to modify them. The goal is to create a makefile such that if a change is made to any of the source code files, the project can be rebuilt by typing make at the command line.

  1. Relevant commands, code, scripts, algorithms:

use vi in unix environment; -o, -I, -c

  1. The attempts at a solution (include all code and scripts):
all: frprog gqprog vwprog

frprog: fr.o
        g++ -o frprog fr.o

fr.o: fr.cc pg.h
        g++ -c fr.cc

gqprog: gq.o
        g++ -o gqprog gq.o

gq.o: gq.cc ac.h bb.h
        g++ -c gq.cc -I/includes/bb.h

vwprog: vw.o
        g++ -o vwprog vw.o

vw.o: vw.cc ac.h
        g++ -c vw.cc

ac.o: ac.cc ac.h pg.h
        g++ -c ac.cc

pg.o: pg.cc pg.h
        g++ -c pg.cc


clean:
        -rm *.o
g++ -o frprog fr.o
fr.o: In function `main':
fr.cc:(.text+0x21): undefined reference to `f1()'
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'frprog' failed
make: *** [frprog] Error 1
make: *** No rule to make target 'bb.h', needed by 'gq.o'.
g++ -o vwprog vw.o
vw.o: In function `main':
vw.cc:(.text+0x21): undefined reference to `x2()'
collect2: error: ld returned 1 exit status
Makefile:16: recipe for target 'vwprog' failed
make: *** [vwprog] Error 1
make: Target 'all' not remade because of errors.
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Northern Illinois University, Dekalb, IL, USA
Dr. Kirk Duffin
CSCI 689

Look... We aren't going to do your homework for you. And make and gcc pretty much tell you exactly what is wrong. So tackle each reported error until you have fixed your makefile to correctly build frprog , gqprog , and vwprog .

So, let's start with your first problem:

Just looking at the above output and your makefile (without even looking at your source files), tells us that fr.cc includes a function called main() and main() calls a function called f1() . And, f1() is not defined in fr.cc .

Do you have another source file that defines f1() ? If so, you need to add the object file produced from that source as a dependency for frprog . If not, you need to write some code that does define it in fr.cc or add it to another file and update the dependencies in your makefile .

Moving on to your second problem... Do you have a header that isn't in the same directory as your source files (and isn't a standard header found in /usr/include ), is there an option you can give to gcc to tell it to look for headers in another directory? If so, update the rules for making the object(s) that depends on that missing header to tell gcc where to find that header when building that(those) object file(s).

And, your third problem looks very similar to your first problem.

Note also that make is very picky about <tab> and <space> characters in rules in makefiles. You have shown us sequences of spaces in your sample makefile that are required to be tab characters. (We can assume that you copied the makefile out of a vi editing window instead of actually copying and pasting your makefile, but be aware that they are not interchangeable. (And, when you don't use CODE tags to show us your makefile, sequences of spaces and tabs are converted to a single space and leading spaces are discarded completely when we try to read your code. So, PLEASE use CODE tags when showing us sample input, sample output, and sample code segments. ICODE tags are fine for partial line snippets of sample code, but multi-line code segments need CODE tags; not ICODE tags.)

I got most of working, just need to ask a question.

I am trying to create the gq.o file but the bb.h is located in another directory with assign1. So its like /assign1/includes/bb.h, how do I define it correctly.

it keeps giving an error saying No rule to make target 'I/assign1/includes/', needed by gq.o. Stop

Thanks

I/assign1/includes/ is a funny looking target. Show us the dependencies and rules in your makefile that mention that target.


output: fr.o vw.o ac.o pg.o gq.o
	g++ -o fr.o gq.o vw.o ac.o pg.o output

fr.o: fr.cc pg.h
	g++ -c fr.cc


vw.o: vw.cc ac.h
	g++ -c vw.cc


ac.o: ac.cc ac.h pg.h
	g++ -c ac.cc


pg.o: pg.cc pg.h
	g++ -c pg.cc


gq.o: gq.cc ac.h -I/assign1/includes bb.h	
	g++ -c gq.cc

clean:
	-rm *.o output

The gq.cc file has include "bb.h". The bb.h file is located in a sub directory under the main directory assign1.

---------- Post updated at 06:07 PM ---------- Previous update was at 06:04 PM ----------

the whole directory location is /home/turing/z1755294/assign1/assign1/includes/bb.h

the 2nd assign1 is where the other source files and header files are located.

The includes directory is what I am trying to accomplish for the gq.o

The following:

gq.o: gq.cc ac.h -I/assign1/includes bb.h	
	g++ -c gq.cc

says that the file gq.o needs to be brought up-to-date if any of the four files:

	gq.cc ac.h -I/assign1/includes bb.h

in the current working directory are newer than gq.o or if any of those files do not exist. Neither of the last two files in that list exist in the current directory, and you have not supplied any rules that tell make how to build those files. The third file in that list looks like it might be an option to gcc to tell it how to find bb.h while it is compiling gq.cc to build gq.o , but it does not tell make how to find bb.h . (But the pathname you specified for bb.h [ /home/turing/z1755294/assign1/assign1/includes ] does not seem to be what is specified by -I/asign1/includes .)

If you are sitting in the directory /home/turing/z1755294/assign1/assign1 when you run make and the files gq.cc and ac.h are in that directory, what pathnames (absolute or relative) can be used to enable make to find the file bb.h ?