LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2005, 01:39 PM   #1
harshnoise
Member
 
Registered: Feb 2005
Location: Newcastle, England
Distribution: SuSE Pro 9.2
Posts: 35

Rep: Reputation: 15
C++ Compiling single class without a main()


Hi very new to C++, trying to compile a single class without an int main() method and I get this error message:

anth@linux:> g++ Employee.cpp -o employee
/usr/lib/gcc-lib/i586-suse-linux/3.3.4/../../../crt1.o(.text+0x18): In function`_start':
../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status

Not a clue why, I've copied it straight from a text book. If I put a main() in it compiles fine. Maybe I shouldn't complain but its just the book does not specify a main so I'd like to know how I can do it without one.

Here is the header file and the implementation just incase.

Thanks guys,

Anthony.


Code:
// This is the header file employee.h
// This is the interface for the class Employee
// This is primarily intended to be used as a base class to derive
// classes for different kinds of employees.

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
using std::string;

namespace QuinnEmployees
{
	class Employee
	{
	public:
		Employee();
		Employee(string theName, string theSsn);
		string getName() const;
		string getSsn() const;
		double getNetPay() const;
		void setName(string newName);
		void setSsn(string newSsn);
		void setNetPay(double newNetPay);
		void printCheck() const;
	private:
		string name;
		string ssn;
		double netPay;
	};
} // QuinnEmployees

#endif // EMPLOYEE_H

Code:
// This is the file Employee.cpp
// This is the implementation for the class Employee.
// The interface for the class Employee is in the header file employee.h

#include <string>
#include <cstdlib>
#include <iostream>
#include "Employee.h"

using std::string;
using std::cout;

namespace QuinnEmployees
{
	Employee::Employee() : name("No name yet"), ssn("No number yet"), netPay(0)
	{
		// Do nothing.
	}
	
	Employee::Employee(string theName, string theNumber) : name(theName), ssn(theNumber), netPay(0)
	{
		// Do nothing.
	}
	
	string Employee::getName() const
	{
		return name;
	}
	
	string Employee::getSsn() const
	{
		return ssn;
	}
	
	double Employee::getNetPay() const
	{
		return netPay;
	}
	
	void Employee::setName(string newName)
	{
		name = newName;
	}
	
	void Employee::setSsn(string newSsn)
	{
		ssn = newSsn;
	}
	
	void Employee::setNetPay(double newNetPay)
	{
		netPay = newNetPay;
	}
	
	void Employee::printCheck() const
	{
		cout << "\n ERROR: printCheck FUNCTION CALLED FOR AN\n"
				 << "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n" 
				 << "Check with the author of the program about this bug.";
		exit(1);
	}
} // QuinnEmployees
 
Old 08-29-2005, 01:55 PM   #2
Meatwad
Member
 
Registered: Dec 2002
Distribution: Debian, Libranet, Red Hat
Posts: 43

Rep: Reputation: 15
Give the "-c" option to g++. By default g++ will compile and link the files you give it. -c tells it just to compile.
 
Old 08-29-2005, 01:57 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I assume your trying to build an object file of which you want to link against other code later. This is done by compiling with the -c option.

g++ -c Employee.cpp -o employee.o

Later on, when you have a class that uses employee.o and you want to link it in (assume the new class does have a main) you'd simply say:

g++ -o NewClass NewClass.cpp employee.o

Keep in mind that you will not be able to run employee.o as it wouldn't do anything in an unlinked state without a main function.

Last edited by jtshaw; 08-29-2005 at 01:58 PM.
 
1 members found this post helpful.
Old 08-29-2005, 02:00 PM   #4
harshnoise
Member
 
Registered: Feb 2005
Location: Newcastle, England
Distribution: SuSE Pro 9.2
Posts: 35

Original Poster
Rep: Reputation: 15
Ahhhh! Thanks guys that makes more sense now. Because I'm not linking it to any actual program. Good, good, I'm learning!

Thanks again.
 
Old 08-29-2005, 04:59 PM   #5
sarunya
LQ Newbie
 
Registered: Aug 2005
Posts: 7

Rep: Reputation: 0
try this

gcc -c Employee.cpp
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
(linking?) problem compiling very simple array class btb Programming 3 08-02-2005 03:16 PM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
Cross Compiling from Single Proc to SMP mikeyt_333 Linux - Software 2 07-12-2004 06:57 AM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
newbie c++ compiling class question true_atlantis Programming 4 02-20-2004 08:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration