Undefined reference to main - collect2: ld returned 1 exit status

后端 未结 7 2156
栀梦
栀梦 2020-12-07 22:43

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/l         


        
相关标签:
7条回答
  • 2020-12-07 23:09

    Executable file needs a main function. See below hello world demo.

    #include <stdio.h>
    int main(void)
    {
            printf("Hello world!\n");
            return 0;
    }
    

    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
    
    0 讨论(0)
提交回复
热议问题