944,048 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 13817
  • C++ RSS
Dec 4th, 2006
0

undefined reference to 'main'

Expand Post »
Alright, people. I am writing a simple, command based program simulatin minesweeper, using a txt file for the map. I am getting characters and stuff in from the file, and converting them to integers to determine the size of the map.

Now all this is going alright, but I am getting a problem involving crt1.o, which i used to start up my computer. I am getting the compiler error: undefined reference to 'main'

I understand that this is a problem relating to the fact that my .cpp file doesn't have a main function.

I could just put one in, but the problem is that this is an implementation file for a header, not a main file. This file isn't supposed to have a main function in it. How should I fix this?

/*This is on my debian 'etch' system, I am using gedit to edit the files and g++ to compile them. my gcc is version 4.1.2. */

[EDIT] I suppose I could just keep writing it, because if that is the only error I get it'll still compile in visual c++ for windoze, and that's what my instructor will be using... [/EDIT]
Last edited by FireSBurnsmuP; Dec 4th, 2006 at 10:29 pm.
Reputation Points: 46
Solved Threads: 2
Posting Whiz in Training
FireSBurnsmuP is offline Offline
237 posts
since Sep 2006
Dec 4th, 2006
0

Re: undefined reference to 'main'

When you run a program, the program starts with the main() function by definition. You need a main() somewhere in your project, and whatever file contains main() should be the one you compile to create your executable. You can just put main in another file (e.g. main.cpp) and that way keep your implementation stuff separate.

Oh, and when I start my computer, I use the power button, not a .o file :lol:
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Dec 4th, 2006
0

Re: undefined reference to 'main'

XD lol yeah I know, I don't personally use that file...

and I know, but I am making this implementation file myself, so don't I have to compile that too, in order for the program using the header to be able to? or will g++ figure it out with the line #include "myheader.h"? The director of the whole cse thing told me to do it this way...
Reputation Points: 46
Solved Threads: 2
Posting Whiz in Training
FireSBurnsmuP is offline Offline
237 posts
since Sep 2006
Dec 4th, 2006
1

Re: undefined reference to 'main'

The #include "myheader.h" will essentially paste the contents of myheader.h into the top of whatever file it is #included into. So it'll be comiled just fine. In order to get the .cpp file compiled, you can just specify it first on the command line, e.g.
C++ Syntax (Toggle Plain Text)
  1. $ g++ myheader.cpp main.cpp
and the linker should take care of everything... I think... (been a li'l while...)
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Dec 4th, 2006
0

Re: undefined reference to 'main'

thanks a lot! I do believe "I owe you one, BPS." You may have saved my life.

( and bravo on the BPS avi, I'm a pretty big fan.)
Reputation Points: 46
Solved Threads: 2
Posting Whiz in Training
FireSBurnsmuP is offline Offline
237 posts
since Sep 2006
Dec 4th, 2006
0

Re: undefined reference to 'main'

Haha, anyone who can recognize BPS deserves a hand... and your ava isn't bad either
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Open a file to an array
Next Thread in C++ Forum Timeline: help: string and random number generator Questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC