468,490 Members | 2,573 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

undefined reference to `main'

I'm making a project involving some C++ source files.
It keeps popping up the following error:

/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../crt1.o(.text+0x18): In
function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status

This happens whether I compile the code using a makefile or normally
(via a g++ command)
Anybody knows what this means?
Thanks.

Oct 6 '06 #1
8 50554

owolablo wrote:
I'm making a project involving some C++ source files.
It keeps popping up the following error:

/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../crt1.o(.text+0x18): In
function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status

This happens whether I compile the code using a makefile or normally
(via a g++ command)
Anybody knows what this means?
Thanks.
Probably that you have failed to include a definition of main.

If you have included such a definition, post it, and we can take a
look.

Best regards,

Tom

Oct 6 '06 #2

owolablo wrote:
I'm making a project involving some C++ source files.
It keeps popping up the following error:

/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../crt1.o(.text+0x18): In
function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status

This happens whether I compile the code using a makefile or normally
(via a g++ command)
Anybody knows what this means?
Thanks.
Probably that you have failed to include a definition of main.

If you have included such a definition, post it, and we can take a
look.

Best regards,

Tom

Oct 6 '06 #3
owolablo wrote:
I'm making a project involving some C++ source files.
It keeps popping up the following error:

/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../crt1.o(.text+0x18): In
function `_start':
>undefined reference to `main'
collect2: ld returned 1 exit status

This happens whether I compile the code using a makefile or normally
(via a g++ command)
Anybody knows what this means?
It means your linker tries to link your program and cannot find the
'main' function, which every program _must_ have. Don't ask your
linker to link if you only compile a single translation unit. See the
-c command-line switch. And next time g++-specific questions should
be asked in gnu.g++.help.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 6 '06 #4
I have actually included a definition to main.My project has two source
files and the main functions in both of them are as follows:

int main (int argc, char *argv[])
{
if(argc<3){
ACE_DEBUG((LM_DEBUG,"Usage %s <hostname<port_number>\n",
argv[0]));
ACE_OS::exit(1);
}
Client client(argv[1],ACE_OS::atoi(argv[2]));
client.connect_to_server();
client.send_to_server();
return 0;
}

and the other one is:

int main (int argc, char *argv[])
{
if(argc<3){
ACE_DEBUG((LM_DEBUG,"Usage %s <hostname<port_number>\n",
argv[0]));
ACE_OS::exit(1);
}
Client client(argv[1],ACE_OS::atoi(argv[2]));
client.connect_to_server();
client.send_to_server();
return 0;
}

Thank you!

Oct 6 '06 #5

owolablo wrote:
I have actually included a definition to main.My project has two source
files and the main functions in both of them are as follows:
And perhaps therin lies the problem. You probably don't want to link 2
main functions together. How does the linker know which one to use as
the entry point?

int main (int argc, char *argv[])
{
if(argc<3){
ACE_DEBUG((LM_DEBUG,"Usage %s <hostname<port_number>\n",
argv[0]));
ACE_OS::exit(1);
}
Client client(argv[1],ACE_OS::atoi(argv[2]));
client.connect_to_server();
client.send_to_server();
return 0;
}

and the other one is:

int main (int argc, char *argv[])
{
if(argc<3){
ACE_DEBUG((LM_DEBUG,"Usage %s <hostname<port_number>\n",
argv[0]));
ACE_OS::exit(1);
}
Client client(argv[1],ACE_OS::atoi(argv[2]));
client.connect_to_server();
client.send_to_server();
return 0;
}

Thank you!
Oct 6 '06 #6
owolablo wrote:
I have actually included a definition to main.My project has two source
files and the main functions in both of them are as follows:
You are only allowed ONE main definition.

If you are writing C++ make sure you are using g++ to
link rather than gcc.

Oct 6 '06 #7
owolablo wrote:
I have actually included a definition to main.My project has two source
files and the main functions in both of them are as follows:
[snip two definitions of main]

You need to quote enough of the post you are responding
to for people to know what it is you are talking about. In
many cases, the previous post may not be available to
people reading your post.

Your problem would seem to be that you have multiple
executables in the same project, and that one or more of
them does not have a main.

You must have exactly one main per executable.

How you get that in a project is not on topic here, as it is
to do with the details of your development platform. It
would be better if you ask such questions in a forum related
to your specific development platform.
Socks

Oct 6 '06 #8
"Undefined reference to main" is actually a good sign because
it means you have no other fatal errors.

Steve
Oct 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Codemutant | last post: by
10 posts views Thread by siroregano | last post: by
1 post views Thread by Andre Janssen | last post: by
8 posts views Thread by wdh3rd | last post: by
2 posts views Thread by zqiang320 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.