哪位高手解释一下,如何应用main,这么做是为什么最近在研究 template 看了 魔兽模拟服务器的代码
这是调用main的地方        ////////////////////////
        // service main cycle //
        ////////////////////////        m_ServiceStatus = 1;
        argc = 1;
        main(argc , argv);// 调用完毕/// Launch the mangos server
extern int main(int argc, char **argv)
{
    // - Construct Memory Manager Instance
    MaNGOS::Singleton<MemoryManager>::Instance();    //char *leak = new char[1000];                          // test leak detection    ///- Command line parsing to get the configuration file name
    char const* cfg_file = _MANGOSD_CONFIG;
    int c=1;
    while( c < argc )
    {
        if( strcmp(argv[c],"-c") == 0)
        {
            if( ++c >= argc )
            {
                sLog.outError("Runtime-Error: -c option requires an input argument");
                usage(argv[0]);
                return 1;
            }
            else
                cfg_file = argv[c];
        }        #ifdef WIN32
        ////////////
        //Services//
        ////////////
        if( strcmp(argv[c],"-s") == 0)
        {
            if( ++c >= argc )
            {
                sLog.outError("Runtime-Error: -s option requires an input argument");
                usage(argv[0]);
                return 1;
            }
            if( strcmp(argv[c],"install") == 0)
            {
                if (WinServiceInstall())
                    sLog.outString("Installing service");
                return 1;
            }
            else if( strcmp(argv[c],"uninstall") == 0)
            {
                if(WinServiceUninstall())
                    sLog.outString("Uninstalling service");
                return 1;
            }
            else
            {
                sLog.outError("Runtime-Error: unsupported option %s",argv[c]);
                usage(argv[0]);
                return 1;
            }
        }
        if( strcmp(argv[c],"--service") == 0)
        {
            WinServiceRun();
        }
        ////
        #endif
        ++c;
    }    if (!sConfig.SetSource(cfg_file))
    {
        sLog.outError("Could not find configuration file %s.", cfg_file);
        return 1;
    }
    sLog.outString("Using configuration file %s.", cfg_file);    ///- and run the 'Master'
    /// \todo Why do we need this 'Master'? Can't all of this be in the Main as for Realmd?
    sMaster.Run();
    return 0;
}/// @}

解决方案 »

  1.   

    main()的返回值,其他文件里要用到编译成obj后,留有变量接口
      

  2.   

    “extern int main(int argc, char **argv) 这么奇怪的语法啊,怎么理解”main是命令行程序的入口函数
    argc是参数个数,即命令行输入的字串个数
    argv保存了若干个参数的指针,如argv[0]保存第一个参数,argv[1]保存第二个
    extern int 返回值主要用于其它程序判断这个程序的返回状态。如果不是批处理运行或被其它程序调用则返回值没有任何作用。
      

  3.   

    main也就是一个函数你也可以直接调用啊
      

  4.   

    main 调用 还是第一次碰到
      

  5.   

    个人觉得这是一个对extern的错误使用。extern是用于声明在其他地方定义的实体的,首先函数从来不需要extern,其次,这里main就是本地定义的,没有任何理由要extern
    网络上垃圾代码很多,不要过分轻信他们
      

  6.   

    mangos中extern int main(..)这样用是有意义的。
    这里的realmd有可能作为Win下的Service运行,而main()函数在这种情况下会在ServiceWin.cpp中的ServiceMain()中被调用。所以必须有extern修饰。大半年过去了,回答一下是希望能对跟我一样刚搜到的朋友有所帮助。mangos是一个相当有价值的程序,跟垃圾代码是有天壤之别的。