user963070
user963070

Reputation: 639

Compile Attempt Gives crt1.o/'start'/undefined reference to 'main'/exit status message

I am working from a book: TCP/IP Sockets in C and its website code.

I am trying to build a client and server based on those files. My make gives lots of error related to not being able to find functions from DieWithMessage.c

Here it is:

  #include <stdio.h>
  #include <stdlib.h>
  #include "Practical.h"

void DieWithUserMessage(const char *msg, const char *detail) {
  fputs(msg, stderr);
  fputs(": ", stderr);
  fputs(detail, stderr);
  fputc('\n', stderr);
  exit(1);
} 

void DieWithSystemMessage(const char *msg) {
  perror(msg);
  exit(1);
}

When I do gcc DieWithMessage.c, I get the following error:

/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function _start': (.text+0x18): undefined reference tomain' collect2: ld returned 1 exit status

How do I compile this by itself so that the errors will stop happening when using the makefile?

Thanks for any help.

Upvotes: 1

Views: 8934

Answers (1)

paxdiablo
paxdiablo

Reputation: 881563

Your C code needs a main function if you're going to try an link/run it. This is a requirement for hosted C applications under the standard.

That error message indicates what's wrong. The C runtime/startup code (CRT) has an entry point of start which sets up the environment then calls your main. Since you haven't provided a main, it complains.

If you only want to generate an object file for later linking with a main (see here for one description of the process), use something like:

gcc -c -o DieWithMessage.o DieWithMessage.c

(-c is the "compile but don't link" flag). You can then link it later with your main program with something like (although there are other options):

gcc -o myProg myProg.c DieWithMessage.o

If you want a placeholder main to update later with a real one, you can add the following to your code:

int main (void) { return 0; }

Upvotes: 5

Related Questions