0

I have:

main.cpp 
distance.cpp
distance.h
adjacencyList.cpp
adjacencyList.h

Here is my makefile:

all: distance main adjacencyList
    g++ distance.o main.o adjacencyList.o

main.o: main.cpp
    g++ main.cpp -lstdc++

adjacencyList.o: adjacencyList.cpp
    g++ adjacencyList.cpp -lstdc++

distance.o: distance.cpp
    g++ distance.cpp -lstdc++

clean:
    rm -rf *.o all

I am getting this error. So I'm pretty sure I'm doing something wrong with main because it is not a class like the other two and does not have a .h file.

enter image description here

Update:

After trying Ben Voigt's solution I am getting 1 error:

enter image description here

7
  • Does the following command work? g++ distance.cpp adjacencyList.cpp main.cpp -lstdc++
    – nrcrast
    Commented Oct 10, 2014 at 5:21
  • No, I get bunch of warnings and then I get 3 errors : main.cpp:27: error asystema was not declared in this scope main.cpp:70: error: aatoia was not declared in this scope
    – Elephant
    Commented Oct 10, 2014 at 5:27
  • @Elephant: Then you need to add #include <stdlib.h> to main.cpp After that, does it work?
    – Ben Voigt
    Commented Oct 10, 2014 at 5:30
  • Also, you eventually want to switch from atoi() to strtol()... the error handling is tons better.
    – Ben Voigt
    Commented Oct 10, 2014 at 5:31
  • I'm still getting a few warnings and 1 error (.text+0x20): undefined reference to 'main' Newline: collect2: ld returned 1` exit status
    – Elephant
    Commented Oct 10, 2014 at 5:38

2 Answers 2

1

Your rules to create object files are missing the -c option, for "compile only". So they are trying to link, and failing because there is no main().

Then, your all target names an executable for each of the compilation units. Again, that's wrong because they don't all have main(). You should have only one executable. all should also be configured as a phony target, because it doesn't build an actual file named all.

All your rules are failing to control the name of the output file.

All your rules are failing to pass flags.

Your rules are missing dependencies on the headers, so editing headers won't cause the right files to be recompiled.

Really, you should get rid of the compile and link rules and let make use its built-in ones. Focus on your build targets and dependencies.

Your end makefile should look something like this (of course, using spaces not tabs)

all : main

.PHONY : all clean

CC = g++
LD = g++

main : main.o adjacencyList.o distance.o

main.o: main.cpp adjacencyList.h distance.h

adjacencyList.o: adjacencyList.cpp adjacencyList.h

distance.o: distance.cpp distance.h

clean:
    rm -rf *.o main
2
  • Thank you, I will look over these points. Since I've only looked at make files for a few hours, I'm basically trying to get a simple makefile working for a homework.
    – Elephant
    Commented Oct 10, 2014 at 5:43
  • I tried your solution and I am still getting one error that I am attempting to solve. It is in my updated question if you have time to look at it. Thanks.
    – Elephant
    Commented Oct 10, 2014 at 5:57
0

Wild guess: it is possible you are missing a semi-colon somewhere before including adjacencyList.h. Check each header files and make sure each class definition is properly terminated with a semi-colon

4
  • Checked: my includes are all like 3rd-4th line down. So no semicolons missing.
    – Elephant
    Commented Oct 10, 2014 at 5:19
  • Good catch Ben. I didn't pay attention to that Commented Oct 10, 2014 at 5:27
  • @elephant, aren't you missing some -c in your makefile (i.e. g++ -c distance.cpp for distance.o)? Commented Oct 10, 2014 at 5:36
  • no, I am using -lstdc++ instead of -c You can look at my make file in my question.
    – Elephant
    Commented Oct 10, 2014 at 5:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.