是'INT为主;“有效的C / C ++程序? [英] Is ‘int main;’ a valid C/C++ program?

查看:116
本文介绍了是'INT为主;“有效的C / C ++程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问,因为我的编译器,似乎这样想,即使我不知道。

I ask because my compiler seems to think so, even though I don’t.

回声'INT为主; | CC -xÇ - -Wall 结果
回声'INT为主; | C ++ -x C ++ - -Wall

锵发出任何警告或错误这一点,和gcc问题只温顺的警告:主通常是一个函数[-Wmain] ,但只有当作为编译C.指定 -std = 似乎并不重要。

Clang issues no warning or error with this, and gcc issues only the meek warning: 'main' is usually a function [-Wmain], but only when compiled as C. Specifying a -std= doesn’t seem to matter.

否则,编译和链接的罚款。但在执行时,用 SIGBUS (对我来说)立即终止。

Otherwise, it compiles and links fine. But on execution, it terminates immediately with SIGBUS (for me).

通过位于什么都要的main()在C和C ++?和快速的grep通过语言规范返回,这肯定会看起来,我认为主要的 的需要的功能。但是从gcc的空话 -Wmain (主是的 一般函数)(和错误的缺乏在这里)似乎可能并非如此。

Reading through the (excellent) answers at What should main() return in C and C++? and a quick grep through the language specs, it would certainly seem to me that a main function is required. But the verbiage from gcc’s -Wmain (‘main’ is usually a function) (and the dearth of errors here) seems to possibly suggest otherwise.

但是,为什么?是否有一些奇怪的边缘案件或为这个历史用?任何人都知道怎么办?

But why? Is there some strange edge-case or "historical" use for this? Anyone know what gives?

我的观点,我想,是我真的觉得这应该是在托管环境中的错误,对吧?

My point, I suppose, is that I really think this should be an error in a hosted environment, eh?

推荐答案

由于该问题是双标记为C和C ++中,推理C ++和C是不同的:

Since the question is double-tagged as C and C++, the reasoning for C++ and C would be different:


  • C ++使用名称重整,以帮助连接不同的类型,如文本上的相同的符号区分一个全局变量 XYZ 和一个独立的全局函数 XYZ(INT)。但是,名称是从来没有错位。

  • C不使用识别码,所以它有可能为一个程序来通过代替不同的符号的设置一个种类的符号混淆连接体,并且具有程序成功链接。

  • C++ uses name mangling to help linker distinguish between textually identical symbols of different types, e.g. a global variable xyz and a free-standing global function xyz(int). However, the name main is never mangled.
  • C does not use mangling, so it is possible for a program to confuse linker by providing a symbol of one kind in place of a different symbol, and have the program successfully link.

这就是这里发生了:链接器能够找到符号,以及它的作用。它线,好像它是一个功能,因为它不知道任何更好该符号。运行库将控制传递到部分要求链接器为,所以连接器给它象征,让链接阶段来完成。当然,这无法在运行时,因为不是一个函数。

That is what's going on here: the linker expects to find symbol main, and it does. It "wires" that symbol as if it were a function, because it does not know any better. The portion of runtime library that passes control to main asks linker for main, so linker gives it symbol main, letting the link phase to complete. Of course this fails at runtime, because main is not a function.

下面是同一个问题的另一个例子:

Here is another illustration of the same issue:

文件x.c:

#include <stdio.h>
int foo(); // <<== main() expects this
int main(){
    printf("%p\n", (void*)&foo);
    return 0;
}

文件y.c:

int foo; // <<== external definition supplies a symbol of a wrong kind

编译:

gcc x.c y.c

这编译,它可能会运行,但它是不确定的行为,因为答应编译器符号的类型从提供给连接器实际的符号有所不同。

This compiles, and it would probably run, but it's undefined behavior, because the type of the symbol promised to the compiler is different from the actual symbol supplied to the linker.

至于警告的推移,我认为是合理的:C,您可以建立一个没有函数库,所以编译器会释放​​名称主如果您需要定义一个变量一些未知的原因。

As far as the warning goes, I think it is reasonable: C lets you build libraries that have no main function, so the compiler frees up the name main for other uses if you need to define a variable main for some unknown reason.

这篇关于是'INT为主;“有效的C / C ++程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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