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++ 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 Commented Oct 10, 2014 at 5:27
  • @Elephant: Then you need to add #include <stdlib.h> to main.cpp After that, does it work? Commented Oct 10, 2014 at 5:30
  • Also, you eventually want to switch from atoi() to strtol()... the error handling is tons better. 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 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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.