C语言 对 main 的未定义引用 - collect2: ld 返回 1 个退出状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7965437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:03:05  来源:igfitidea点击:

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

cgccreference

提问by noobsharp

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

我正在尝试编译一个程序(称为 es3),但是,当我从终端写入时:

gcc es3.c -o es3

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?

我能做什么?

回答by Blagovest Buyukliev

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

这意味着es3.c它没有定义一个main函数,而您正试图从中创建一个可执行文件。可执行文件需要有一个入口点,因此链接器会抱怨。

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

要仅编译为目标文件,请使用以下-c选项:

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

The above compiles es3.cto an object file, then compiles a file main.cthat would contain the mainfunction, and the linker merges es3.oand main.ointo an executable called es3.

上述编译es3.c成目标文件,然后编译一个文件main.c将包含所述main功能,并且所述接头合并es3.omain.o成叫做可执行es3

回答by Basile Starynkevitch

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

也许你的main函数因为预处理而被注释掉了。要了解预处理在做什么,请尝试gcc -C -E es3.c > es3.i使用编辑器查看生成的文件 es3.i(并main在其中搜索)。

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 -Wallflag 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.Ctill you got no more warnings.

-Wall标志是非常重要的,你应该永远使用它。它告诉编译器给你(几乎)所有的警告。并且您应该始终听取警告,即更正您的源代码文件,es3.C直到您不再收到警告为止。

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

-g标志也很重要,因为它要求gcc将调试信息放入目标文件和可执行文件中。然后您就可以使用调试器(如gdb)来调试您的程序。

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

要获取目标文件或可执行文件中的符号列表,您可以使用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).

当然,我假设您使用的是 GNU/Linux 系统(如果您尚未使用 GNU/Linux,我邀请您使用它)。

回答by matthewatabet

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.

到目前为止尚未提及的一种可能性是您可能没有编辑您认为的文件。即您的编辑器可能与您的想法有不同的 cwd。

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!

在您正在编译的文件上运行“更多”以仔细检查它是否确实包含您希望的内容。希望有帮助!

回答by Daniel

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

可执行文件需要一个 main 函数。请参阅下面的 hello world 演示。

#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' "

如您所见,有一个主要功能。如果你没有这个 main 函数,ld 会报告“对 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.oto check if there is a main symbol. Below is my result.

请用于$ objdump -t es3.o检查是否有主符号。下面是我的结果。

$ 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

回答by Daniel

I my case I found out the voidfor the main function declaration was missing.

我的情况是我发现void缺少 main 函数声明。

I was previously using Visual Studio in Windows and this was never a problem, so I thought I might leave it out now too.

我以前在 Windows 中使用 Visual Studio,这从来都不是问题,所以我想我现在也可以不使用它。

回答by ƶƶƶƶɮ

You can just add a mainfunction to resolve this problem. Just like:

您可以添加一个main函数来解决此问题。就像:

int main()
{
    return 0;
}

回答by Krishna Salampuriya

In my case it was just because I had not Saved the source file and was trying to compile a empty file .

就我而言,这只是因为我没有保存源文件并试图编译一个空文件。