Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Undefined reference to main

Status
Not open for further replies.

ktulu2

Technical User
Jan 16, 2003
11
US
I am currently learning C++. I am going through the book "Beginning c++" from Wrox Press. All the stupid little programs I have written so far have worked flawlessly. But now I am in the OOP section and they are dividing up the code into different files. The program I am having problems with has a class to make a 'box' object. I have 3 files, Box.h, Box.cpp, & UseBox.cpp. I copied the code straight fromt he book. Here it is.
Code:
// Box.h
#ifndef BOX_H
#define BOX_H

class Box
{
	public:
		double length;
		double breadth;
		double height;
		
		// Constructor
		Box(double lengthValue, double breadthValue, double heightValue);
		
		// Function to calculate volume of box
		double volume();
};
#endif
Code:
// Box.cpp
#include <iostream>
#include "Box.h"

using namespace std;

// Constructor definition
Box::Box(double lengthValue, double breadthValue, double heightValue)
{
	cout << "Box constructor called" << endl;
	length = lengthValue;
	breadth = breadthValue;
	height = heightValue;
}

// Function to calculate the volume of a box
double Box::volume()
{
	return length * breadth * height;
}
Code:
// UseBox.cpp - Program to use class Box

#include <iostream>
#include "Box.h"

using namespace std;

int main()
{
	Box firstBox(80.0, 50.0, 40.0);
	cout << endl;
	cout << "Size of the first box object is "
			<< firstBox.length   << " by "
			<< firstBox.breadth << " by "
			<< firstBox.height
			<< endl;
	cout << "Volume of first Box object is " << firstBox.volume()
			<< endl;
	
	return 0;
}
The author has not discussed how to write makefiles yet so I read some online tutorials and came up with this...
Code:
CXX = g++
CCFLAGS = -g -Wall

MyProj: UseBox.o Box.o
	${CXX} ${CCFLAGS} -o UseBox.o Box.o

Box.o: Box.cpp Box.h
	${CXX} ${CCFLAGS} -c Box.cpp

UseBox.o: UseBox.cpp Box.cpp Box.h
	${CXX} ${CCFLAGS} -c UseBox.cpp

When I do 'make' I get...
g++ -g -Wall -c UseBox.cpp
g++ -g -Wall -o UseBox.o Box.o
/usr/lib/gcc-lib/i586-suse-linux/3.3.3/../../../crt1.o(.text+0x18): In function `_start':
../sysdeps/i386/elf/start.S:98: undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [MyProj] Error 1

Anyone have any ideas? I have been searching google endlessly but have not found anything useful. I'm guessing it is some stupid error in my makefile.

Much thanks for any help.
I appologize for the total noob question.

Cory Tobin
 
Try
Code:
CXX = g++
CCFLAGS = -g -Wall

MyProj: UseBox.o Box.o
    ${CXX} ${CCFLAGS} -o MyProj UseBox.o Box.o

Box.o: Box.cpp Box.h
    ${CXX} ${CCFLAGS} -c Box.cpp

UseBox.o: UseBox.cpp Box.h
    ${CXX} ${CCFLAGS} -c UseBox.cpp

--
 
Code:
g++ -g -Wall [COLOR=red] -o UseBox.o [/color] Box.o

The -o says "name the executable the next thing on the command line"

So your UseBox.o becomes the output file and Box.o has no main (because it is compiled into UseBox.o).

The solution Salem posted will create a program called MyProj and should work for you; and now you know why.
 
UseBox.o: UseBox.cpp Box.h
${CXX} ${CCFLAGS} -c UseBox.cpp

the -c should not be there.
 
Awesome! Thanks very much! It worked perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top