对 main 的未定义引用 - collect2: ld 返回 1 个退出状态 [英] Undefined reference to main - collect2: ld returned 1 exit status

查看:30
本文介绍了对 main 的未定义引用 - collect2: ld 返回 1 个退出状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

gcc es3.c -o es3

出现此消息:

/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

我能做什么?

推荐答案

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

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.

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

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

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

以上将 es3.c 编译为目标文件,然后编译包含 main 函数的文件 main.c,以及链接器将 es3.omain.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.

这篇关于对 main 的未定义引用 - collect2: ld 返回 1 个退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆