Undefined reference [SOLVED]

My wife and I have a problem with the contents of our refrigerator. Fridge is full of stuff, and we have very little idea of what's in there/how long it's been in there. I had the idea of creating some kind of inventory system to keep track of what goes in the fridge and when it should be removed, etc, etc. Granted, I just started designing this solution today, so it's not very well fleshed out. I'm sure that over time (as I gain more experience with designing these things) my skill will improve.

My design:

I was going to have a single Fridge object, holding multiple FridgeItem* objects. I created my two classes, and a main.cpp to call them. I then copied over a Makefile from a project I was working on a LONG time ago, and altered it to reflect my current project.

Here are my files (so far):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef _fridgeitem_
#define _fridgeitem_

#include <string>

class FridgeItem
{
   public:
      FridgeItem();
      ~FridgeItem();
      std::string getName();
      int getCount();

   private:
      std::string name;

};
#endif
michael@caitlyn fridge $ cat FridgeItem.cpp 
#include "FridgeItem.h"

FridgeItem::FridgeItem()
 {
 }

FridgeItem::~FridgeItem()
{
}
michael@caitlyn fridge $ cat Fridge.h 
#ifndef _FRIDGE_
#define _FRIDGE_
#include "FridgeItem.h"

class Fridge
{
   public:
      Fridge();
      ~Fridge();
      bool add(FridgeItem* item);
      //bool remove
      int getCount(); //Returns total number of FridgeItems
      void printCount(); //Prints number returned by getCount;

   private:
      int count;

};


#endif
michael@caitlyn fridge $ cat Fridge.cpp 
#include "Fridge.h"
#include <iostream>
using namespace std;

Fridge::Fridge() 
{
   count = 0;
   cout << "I'm here!" << endl;
}
Fridge::~Fridge()
{
}


bool Fridge::add(FridgeItem* item)
{
   //Other add stuff
   count++; //Increase the counter last of all
}

//bool Fridge::remove


int Fridge::getCount() //Returns total number of FridgeItems
{
   return count;
}

void Fridge::printCount()
{
   cout << "Number of items currently in the fridge:  " << getCount() << endl;
}

michael@caitlyn fridge $ cat main.cpp 
#include "Fridge.h"


Fridge* fridge;

int main()
{
   fridge = new Fridge();
   fridge->printCount();

//   delete fridge; //We must clean up after ourselves.
   return 0;
}

michael@caitlyn fridge $ cat Makefile 
CXX=g++

CFLAGS=-ggdb -H -W -Wall -pedantic -dH   

LIBS=

OBJS = Fridge.o FridgeItem.o

INCS = Fridge.h FridgeItem.h

all: main 

main: $(OBJS)
	$(CXX) -o $@ $(OBJS) $(LIBS)

%.o : %.cpp $(INCS)
	$(CXX) $(CFLAGS) -c $<

clean:
	-rm *.o *~ core* vgcore* main


Now, the problem I am having:

1
2
3
4
5
6
7
8
9
michael@caitlyn fridge $ make
g++ -o main Fridge.o FridgeItem.o 
/usr/lib64/gcc/x86_64-slackware-linux/5.5.0/../../../../lib64/crt1.o: In function `_start':
/glibc-tmp-95e0907d753adfc89f1c416310f2df8c/glibc-2.23/csu/../sysdeps/x86_64/start.S:114: undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:14: recipe for target 'main' failed
make: *** [main] Error 1
michael@caitlyn fridge $ 


I'm going to go do more digging into this undefined reference thing, because I feel like I used to know how to fix it myself.
Last edited on
I previously compiled my Fridge and FridgeItem classes to object files. I found on another website about linker errors that I could issue a
 
g++ FridgeItem.o Fridge.o main.cpp -o main


I was expecting some similar error to before, but it didn't happen. There was no output. I ran ./main and the Fridge constructor spit out it's line, "I'm here!" and the fridge object in main spit out the results of its printCount() member function. I deleted main and attempted to remake it with the make command. It failed, but I noticed something.
1
2
3
4
5
6
7
8
9
g++ -o main Fridge.o FridgeItem.o 
/usr/lib64/gcc/x86_64-slackware-linux/5.5.0/../../../../lib64/crt1.o: In function `_start':
/glibc-tmp-95e0907d753adfc89f1c416310f2df8c/glibc-2.23/csu/../sysdeps/x86_64/start.S:114: undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:14: recipe for target 'main' failed
make: *** [main] Error 1
michael@caitlyn fridge $ 

The order of files in the g++ command has main first.  Shouldn't main be last? 
OBJS = Fridge.o FridgeItem.o
No main.o in the list. Surely you want the code in main.cpp to be used too?
OK. I've altered my Makefile to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CXX=g++

CFLAGS=-ggdb -H -W -Wall -pedantic -dH 


OBJS = FridgeItem.o Fridge.o

INCS = FrideItem.h Fridge.h

default: main 

main:
	g++ FridgeItem.o Fridge.o main.cpp -o main

%.o : %.cpp $(INCS)
	$(CXX) $(CFLAGS) -c $<

clean:
	-rm *.o *~ main


and now it works as I expect it to. Score one for the Rubber Ducky method...
Why not
1
2
3
4
5
6
OBJS = Fridge.o FridgeItem.o main.o

all: fridge

fridge: $(OBJS)
	$(CXX) -o $@ $(OBJS) $(LIBS)
Topic archived. No new replies allowed.