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]

Recommended Answers

All 5 Replies

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:

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

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.

$ g++ myheader.cpp main.cpp

and the linker should take care of everything... I think... (been a li'l while...)

commented: Like I said, you might have saved my life. +1

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

Haha, anyone who can recognize BPS deserves a hand... and your ava isn't bad either ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.