在C,不是主要需要是一个函数? [英] In C, main need not be a function?
问题描述
这code编译,但没有惊喜,它同时连接失败(无主找到):
This code compiles, but no surprises, it fails while linking (no main found):
清单1:
void main();
链接错误:\\ MinGW的\\ LIB \\ libmingw32.a(main.o中):main.c中:(文字+ 0x106)未定义的引用_WinMain @ 16
Link error: \mingw\lib\libmingw32.a(main.o):main.c:(.text+0x106) undefined reference to _WinMain@16'
不过,在code编译以下和链接细末,用一个警告:
But, the code below compiles and links fine, with a warning:
清单2:
void (*main)();
警告:'主'通常是一个函数
warning: 'main' is usually a function
问题:
-
在清单1中,连接器应该有
抱怨失踪主。为什么
它是寻找_WinMain @ 16?
In listing 1, linker should have complained for missing "main". Why is it looking for _WinMain@16?
从生成的可执行
上市2只是崩溃。什么是
什么原因呢?
The executable generated from listing 2 simply crashes. What is the reason?
感谢您的时间。
推荐答案
1,案例是针对Windows的 - 编译器可能会生成 _WinMain
符号时主
正确定义。
Case 1. is Windows-specific - the compiler probably generates _WinMain
symbol when main
is properly defined.
案例2 - 你有一个指针,但作为静态变量的的它初始化为零的,从而坠毁
Case 2. - you have a pointer, but as static variable it's initialized to zero, thus the crash.
这篇关于在C,不是主要需要是一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!