Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to compile a program (called es3), but, when I write from terminal:

gcc es3.c -o es3

it appears this message:

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

What could I do?

@BrandoSK: When I follow that link, I get a 45-second countdown. Either post the code as part of your question, or narrow it down to a small example that illustrates the problem and post that. – Keith Thompson Nov 2, 2011 at 3:58 Ok, I just downloaded your source file. (1) It's called es3_f3.c, not es3.c. (2) It contains multiple errors, for example a missing semicolon on line 18. It could not possibly have compiled, which means you couldn't get far enough to see the linker error that you report. Show us your real code. (The downloaded source file is 73 lines, which is short enough to post here directly.) – Keith Thompson Nov 2, 2011 at 4:03 The error message you show is very similar to the one I get when I use the command gcc es3.c -o es3 with a source file that doesn't define a main function. Whatever source file you're really compiling doesn't define a main function. – Keith Thompson Nov 2, 2011 at 4:07 Perhaps you screwed your compiler installation, or you want some cross compiler. Check also your $PATH – Basile Starynkevitch Feb 15, 2017 at 5:58

It means that es3.c does not define a main function, and you are attempting to create an executable out of it. An executable needs to have an entry point, thereby the linker complains.

To compile only to an object file, use the -c option:

gcc es3.c -c
gcc es3.o main.c -o es3

The above compiles es3.c to an object file, then compiles a file main.c that would contain the main function, and the linker merges es3.o and main.o into an executable called es3.

the problem is that I put the main function inside my program.. I don't understand why It says that there's no main function... :/ – noobsharp Nov 1, 2011 at 12:27

Perhaps your main function has been commented out because of e.g. preprocessing. To learn what preprocessing is doing, try gcc -C -E es3.c > es3.i then look with an editor into the generated file es3.i (and search main inside it).

First, you should always (since you are a newbie) compile with

  gcc -Wall -g -c es3.c
  gcc -Wall -g es3.o -o es3

The -Wall flag is extremely important, and you should always use it. It tells the compiler to give you (almost) all warnings. And you should always listen to the warnings, i.e. correct your source code file es3.C till you got no more warnings.

The -g flag is important also, because it asks gcc to put debugging information in the object file and the executable. Then you are able to use a debugger (like gdb) to debug your program.

To get the list of symbols in an object file or an executable, you can use nm.

Of course, I'm assuming you use a GNU/Linux system (and I invite you to use GNU/Linux if you don't use it already).

in my case i opened file earlier from one directory into gedit and than move it to another directory and try to compile from new directory that i recently moved. but file is empty because it already open. – SidPro Oct 10, 2020 at 8:44

As you can see there is a main function. if you don't have this main function, ld will report "undefined reference to main' "

check my result:

$ cat es3.c
#include <stdio.h>
int main(void)
    printf("Hello world!\n");
    return 0;
$ gcc -Wall -g -c es3.c
$ gcc -Wall -g es3.o -o es3
~$ ./es3
Hello world! 

please use $ objdump -t es3.o to check if there is a main symbol. Below is my result.

$ objdump -t es3.o
es3.o:     file format elf32-i386
SYMBOL TABLE:
00000000 l    df *ABS*  00000000 es3.c
00000000 l    d  .text  00000000 .text
00000000 l    d  .data  00000000 .data
00000000 l    d  .bss   00000000 .bss
00000000 l    d  .debug_abbrev  00000000 .debug_abbrev
00000000 l    d  .debug_info    00000000 .debug_info
00000000 l    d  .debug_line    00000000 .debug_line
00000000 l    d  .rodata        00000000 .rodata
00000000 l    d  .debug_frame   00000000 .debug_frame
00000000 l    d  .debug_loc     00000000 .debug_loc
00000000 l    d  .debug_pubnames        00000000 .debug_pubnames
00000000 l    d  .debug_aranges 00000000 .debug_aranges
00000000 l    d  .debug_str     00000000 .debug_str
00000000 l    d  .note.GNU-stack        00000000 .note.GNU-stack
00000000 l    d  .comment       00000000 .comment
00000000 g     F .text  0000002b main
00000000         *UND*  00000000 puts
                thank for your answers, but I have the main function inside my program :(  don't understand why the compiler doesn't read it :(
– noobsharp
                Nov 1, 2011 at 12:26
                I believe you don't really have a main; it is obscured, skipped by the preprocessor, made internal or static... Try to indent your file, it will be more readable to you...
– Basile Starynkevitch
                Dec 2, 2011 at 6:40
                @Basile Starynkevitch: I have a main. But I think BrandoSK don't have the main function. What do you think?
– Daniel
                Dec 2, 2011 at 6:55
                Yes, I was speaking to the original poster, BrandoSK. When the compiler says no main, he should trust the compiler!!
– Basile Starynkevitch
                Dec 2, 2011 at 7:02

One possibility which has not been mentioned so far is that you might not be editing the file you think you are. i.e. your editor might have a different cwd than you had in mind.

Run 'more' on the file you're compiling to double check that it does indeed have the contents you hope it does. Hope that helps!