First time programmer needs Help with Makefiles

I am trying to practice to create Makefiles. 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.

I have the following files:

ac.cc: has include ac.h and pg.h
fr.cc:  has main function and include pg.h
gq.cc:  has main function and include ac.h, bb.h
pg.cc:  has include pg.h
vw.cc:  has main function and include ac.h

This is the error I keep getting:

Makefile:32: warning: overriding recipe for target 'pg.o'
Makefile:8: warning: ignoring old recipe for target 'pg.o'
g++ -c ac.cc
g++ -c pg.cc
g++ -o acprog ac.o pg.o
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o: In function `_start':
/build/glibc-Ir_s5K/glibc-2.19/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'acprog' failed
make: *** [acprog] Error 1
z1755294@hopper:~/assign1/assign1$ 

This is my Makefile code:

acprog: ac.o pg.o
        g++ -o acprog ac.o pg.o

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

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

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

pgprog: pg.o
        g++ -o pgprog pg.o

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

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

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

clean:
        -rm *.o

What exactly am I doing wrong and how can I fix this to make it work.

Thanks

The warnings at the start of your output are there because you have two rules in your make file for building pg.o (both shown in red above). Since you said that pg.cc includes pg.h (but not ac.h ), get rid of the first rule in your makefile for building pg.o that starts on line 8 in your makefile.

All runnable C++ programs must include one function named main() (which is the function that will be called when your program starts execution). Your build is failing because neither pg.cc nor ac.cc contain such a function.

You might be interested in this little introduction on makefiles.

I hope this helps.

baunin

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

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



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



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

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

clean:
        -rm *.o


Can't seem to create other source files in my program.

Makefiles are usually used to create object files; not source files. And, I don't see any rules in your makefile that make any attempt to create a source file.

What source files are you trying to create with your makefile ?

What command line did you use when you invoked make ?

What output did you get when you ran make ?

What files were you hoping your invocation of make would produce?

Did you make any changes to any of your source files from the way you described them in post #1 in this thread (such as adding a definition of the function main() in ac.cc or pg.cc )?

Here is make file:

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

I have attached the source files we need to use to create a Makefile. The purpose is to create a makefile that if a change is made to any of the source files, the project can be rebuilt by typing make at the command line.

Any help would be appreciated to complete this. I have been struggling with this.

We are not allowed to modify any of the source files

here are the errors I get when I type in make at the command line:

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.
Moderator comments were removed during original forum migration.