MS C++/C应用程序的启动函数为mainCRTStartup()或者WinMainCRTStartup(), 他们都在这个函数内部定义了main()函数,或者WinMain(),然后以他们的返回值为参数调用库函数exit(),因此也就默认了main()应该作为它的连接对象,如果找不到这样的函数定义,自然就会报错了。
main()其实就像是一个回调函数。
基于应用程序的框架生成的源代码中往往找不到main(),因为应用程序框架把main()的实现隐藏起来了,并且它的实现具有固定的模式,所以不需要程序员来编写。在应用程序的连接阶段,框架会将包含main()实现的library加进来一起连接。
我们写的可执行程序如何处理命令行参数的能力? 可以在main()函数中添加形式参数以接收程序在启动时从命令行中输入的各个参数。如int main(int argc, char* argv[]) 输入的命令和参数个数加起来传给argc, 程序截获并打包成字符串数组后传递给argv.
以下程序说明了这点:
#include<stdio.h>
int main(int argCount,char* argValue[])
{
FILE *srcFile=0,*destFile=0;
int ch=0;
if(argCount!=3)
{
printf("USage:%s src-file-name dest-file-name/n",argValue[0]);
}
else
{
if((srcFile=fopen(argValue[1],"r"))==0)
{
printf("Can not open source file/"%s/"!",argValue[1]);
}
else
{
if((destFile=fopen(argValue[2],"w"))==0)
{
printf("Can not open destination file/"%s/"!",argValue[2]);
fclose(srcFile);
}
else
{
while((ch=fgetc(srcFile))!=EOF)
fputc(ch,destFile);
printf("Successful to copy a file!/n");
fclose(srcFile);
fclose(destFile);
return 0;
}
}
}
return 1;
}