Compiling with int main(void) fails; main(int argc, char *argv[]) succeeds. Why?(用 int main(void) 编译失败;main(int argc, char *argv[]) 成功.为什么?)
问题描述
为什么编译具有 int main(void) 主函数的程序与编译具有 int main(int argc, char *argv[]) 的程序不同 main 函数,如果程序不使用命令行传递的参数?
Why would compiling a program which has an int main(void) main function differ from compiling a program which has an int main(int argc, char *argv[]) main function, if the program does not use arguments passed on the command line?
这个操作系统或编译器是特定的吗?我使用 mingw 和 g++ 没有得到相同的结果(这很奇怪,因为wingw 是 gcc 的一个端口).
Is this OS or compiler specific? I do not get the same results using mingw and g++(which is weird isn't it as wingw is a port of gcc).
#include <iostream>
#include"SDL/SDL.h"
int main(void)
{
return 0;
}
编译命令
g++ test.cpp; #g++ 4.4.5
i586-mingw32msvc-g++ test.cpp; # mingw 4.4.4
错误
(由第二个命令给出.)
Error
(Given by the second command.)
a(main.o):(.text+0x85): undefined reference to `_WinMain@16'
推荐答案
这是 SDL 的事情.在 Windows 上,当你包含 SDL.h 时,main 被重新定义为调用 WinMain 的 SDL_main(真正的入口指向非控制台 Windows 应用程序),进行一些初始化,最后调用您的主代码.它有一个带有 argc 和 argv 的签名,你几乎需要遵循它,所以 int main() 不起作用.
This is SDL thing. On Windows, when you include SDL.h,main is redefined to SDL_main which calls WinMain (the real entry point in non-console Windows apps), does some initialization and finally calls your main code. It has a signature with argc and argv and you're pretty much required to follow it, so int main() won't work.
这篇关于用 int main(void) 编译失败;main(int argc, char *argv[]) 成功.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用 int main(void) 编译失败;main(int argc, char *argv[]
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
