未定义的对"main"的引用在最小的C程序中 [英] Undefined reference to "main" in minimal C program

查看:89
本文介绍了未定义的对"main"的引用在最小的C程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更深入地了解C编​​译,因此我正在手动"进行编译和链接.这是我的代码

I'm trying to understand C compilation in a little more depth, and so I'm compiling and linking "manually". Here is my code

int main()
{
    return 0;
}

这是我要放入控制台(Windows)的内容:

And here is what I'm putting into my console (Windows):

gcc -S main.c
as main.s -o main.o
ld main.o

当尝试链接时,我得到:

And when trying to link, I get:

main.o:main.c:(text+0x7): undefined reference to `__main'

推荐答案

您没有链接任何必要的支持库.诸如stdin,stdout,stderr之类的C全局对象不会无处不在.命令自变量和环境变量从操作系统中提取.并在退出时调用所有这些atexit()函数,并将来自main的返回代码传递给exit(return_code).等等.

You didn't link any of the necessary support libraries. C global objects like stdin, stdout, stderr don't just appear from nowhere. Command arguments and environment variables are pulled from the operating system. And on exit all those atexit() functions get called and the return code from main is passed to exit(return_code). Etc.

检查命令gcc -dumpspecsgcc -print-libgcc-file-name.查看该目录中的所有其他库.您会在dumpspecs的输出中找到很多引用的库和目标文件.我不确切知道何时或如何解释这些规范规则,但您可能可以理解.而且我认为,如果您深入了解,GCC信息页info gcc会对其进行详细说明.

Check out the commands gcc -dumpspecs, gcc -print-libgcc-file-name. Look at all of the other libraries in that directory. You'll find a lot of those libraries and object files referenced in the output of dumpspecs. I don't know exactly when or how those spec rules are interpreted but you can probably get the idea. And I think the GCC info pages info gcc explain it in detail if you dig in far enough.

info gcc,然后按'g',然后输入'Spec Files'

info gcc and then press 'g' and then enter 'Spec Files'

正如乔纳森·莱夫勒(Jonathan Leffler)所说,快捷方式是使用详细选项运行 gcc,然后看看它使用了什么命令.

And as Jonathan Leffler said, the shortcut is to run gcc with the verbose option: gcc -v and just see what commands it used.

这篇关于未定义的对"main"的引用在最小的C程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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