首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c/c++基准有没有规定程序的入口点必须是main()函数

2013-12-11 
c/c++标准有没有规定程序的入口点必须是main()函数?还是说,可以是别的函数名?这个c/c++标准规定了吗?[解决

c/c++标准有没有规定程序的入口点必须是main()函数?
还是说,可以是别的函数名?
这个c/c++标准规定了吗?
[解决办法]
5.1.2.2.1 Program startup

1  The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;or in some other implementation-defined manner.

2  If they are declared, the parameters to the main function shall obey the following
constraints:
— The value of argc shall be nonnegative.
— argv[argc] shall be a null pointer.
— If the value of argc is greater than zero, the array members argv[0] through
argv[argc-1] inclusive shall contain pointers to strings, which are given
implementation-defined values by the host environment prior to program startup. The
intent is to supply to the program information determined prior to program startup
from elsewhere in the hosted environment.  If the host environment is not capable of
supplying strings with letters in both uppercase and lowercase, the implementation
shall ensure that the strings are received in lowercase.
— If the value of argc is greater than zero, the string pointed to by argv[0]
represents the program name; argv[0][0]shall be the null character if the
program name is not available from the host environment.  If the value of argc is
greater than one, the strings pointed to by argv[1] through argv[argc-1]
represent the program parameters.
— The parameters argc and argv and the strings pointed to by the argv array shall
be modifiable by the program, and retain their last-stored values between program
startup and program termination.
[解决办法]
在VS中可以设置:
#pragma   comment(   linker,   "/subsystem:\ "windows\ "   /entry:\ "mainCRTStartup\ " "   )   //   设置入口地址

参考下面的
在控制台程序中隐藏控制台窗口! 
大家都知道,当编写一个win32   console   application时,当运行此类程序的时候 
默认情况下会有一个类似DOS窗口的console窗口,但是有的时候我们只想在程序 
中运行一段功能代码,不希望显示这个console窗口,让代码执行完毕之后程序自 
动退出. 

下面就介绍一下,如何隐藏win32   console   application的console窗口 

因为此种方法是通过设置编译器的链接开关来实现,所以让我们来看一下编译 
器的链接开关选项(也就是linker选项). 

首先我们来看一下linker的   /subsystem   选项 

该选项的语法形式如下: 
/SUBSYSTEM:{CONSOLE
[解决办法]
EFI_APPLICATION
[解决办法]
EFI_BOOT_SERVICE_DRIVER
[解决办法]
 
                        EFI_ROM
[解决办法]
EFI_RUNTIME_DRIVER
[解决办法]
NATIVE
[解决办法]
POSIX
[解决办法]
WINDOWS
[解决办法]
WINDOWSCE} 
                        [,major[.minor]] 



这个链接选项告诉操作系统如何运行可执行文件 

CONSOLE: 
win32   字符模式应用程序,此种类型的应用程序在运行的时候会产生一个类似DOS 
窗口的控制台窗口,如果在应用程序的主函数为main()或者wmain(),在默认情况下 
该应用程序就是一个控制台应用程序 

Extensible   Firmware   Interface 
和CPU具体架构相关的一个参数选项,并不常用,在这里暂不详细介绍. 
如果对此有兴趣的可以访问intel主页来查看相关内容 

NATIVE; 
设备驱动器选项,如果/DRIVER:WDM选项被设定的话,该链接选项(NATIVE)就为默认选项 

POSIX: 
在windows   NT   种运行在POSIX子系统上的应用程序 

WINDOWS: 
该类型的应用程序不产生console窗口,该类型的应用程序的窗口由用户自己创建,简而言之 
就是一个标准的Win32   application,其入口地址为WinMain()函数或者wWinMain()函数的地址 
如果你在应用程序种定义的主函数为WinMain或者wWinMain,在默认情况下该应用程序就是一个 
Win32   Application   ! 

WINDOWSCE: 
运行在windows   CE上的应用程序 

major   and   minor   (optional): 
主版本号和次版本号,该选项为可选,该选项为0~65535之间的十进制整数   

从上面可以看出如果我们建立一个win32   console   application的话,linker的/subsystem选项应该为 
CONSOLE,可以在VC开发环境的project-> setting-> link-> project   option中看到! 

接下来我们再看看应用程序是如何运行的! 
我们知道用VC编写的程序,运行的时候是需要   C\C++运行库支持的.当我们运行一个C/C++程序的时候
链接器会首先寻找应用程序的启动函数,例如: 
如果你建立了一个console程序的话,编译器得链接开关会是以下这种形式 
/subsystem: "console "   /entry: "mainCRTStartup "       (ANSI) 
/subsystem: "console "   /entry: "wmainCRTStartuup "   (UNICODE) 

如果你建立了一个win32   application,编译器得链接开关则会是一下形式 
/subsystem: "windows "   /entry: "WinMain "       (ANSI) 
/sbusystem: "windows "   /entry: "wWinMain "     (UINCODE) 

上面的两种形式可以再project-> setting-> link-> project   option中看到 
上面的subsystem和entry并不需要都设置,如果你只设置了/subsystem: "console " 
的话,那么默认的entry开关在默认情况下应为/entry: "mainCRTStartup " 
反之,如果你在应用程序中定义了main函数的话,默认情况下,你的/subsystem开关 
应该为/system: "console " 


在默认情况下/subsystem   和/entry开关是匹配的,也就是 
console对应mainCRTStartup或者wmainCRTStartup 
windows对应WinMain或者wWinMain 

但是我们也可以通过手动改动的方式使他们不匹配 


例如我们可以这样改动 

#pragma   comment(   linker,   "/subsystem:\ "windows\ "   /entry:\ "mainCRTStartup\ " "   )   //   设置入口地址   


int   main(int   argc,   char*   argv[]) 

  MessageBox(NULL,   "hello ",   "Notice ",   MB_OK); 
  return   0; 


在默认情况下链接器看到/subsystem下是windows选项的时候,它会自动寻找WinMain或者wWinMain 

但我们强制指定入口地址,这样运行程序的时候默认的console窗口就会隐藏! 


上面是在代码中使用#pragma指令来设置,还有一种就是直接在开发环境的 
project-> setting-> link-> project   option中手工改动! 

写了这么多,自己都有点感觉乱,没有办法,以前没写过什么文章,所以措辞可能不太好,希望大家见
[解决办法]
c/c++基准有没有规定程序的入口点必须是main()函数
核心编程上这么说  能看出什么。。。
[解决办法]
《Windows PE权威指南》

热点排行