AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > C++

送给用 EditPlus 或 Programmer's Notepad 等编辑器写代码的朋友!

51自学网 http://www.51zixue.net

原帖及讨论:http://bbs.bccn.net/thread-232807-1-1.html

昨天特意写的一个集编译连接和运行为一体的一个脚本!
只能编译 C/ C++ 代码, 当然前提是你得有编译系统!

/*
    * DocumentName: CompileAndLinker.js
    *   CreateDate: 2009-9-11
    *        Author: BlueMouse, [Lin], [Weipengyuan], [阿远]
    *          Note: 编译和连接运行和运行C或CPP代码
    *    CallFormat: CompileAndLinker.js "编译程序的路径", "连接程序的路径", "源文件所在的目录", "编译和连接后输出的目录", "连接后生成的可执行文件名", "连接开关", "头文件目录表", "库文件目录表"
    *  CallExample: cscript.exe CompileAndLinker.js "D:/Dev-Cpp/Bin/g++.exe" "D:/Dev-Cpp/Bin/g++.exe" "E:/Programs/Example" "E:/Programs/Example/Output" "MyProgram" "-mwindows" "D:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include|D:/Dev-Cpp/include/c++/3.4.2/backward|D:/Dev-Cpp/include/c++/3.4.2/mingw32|D:/Dev-Cpp/include/c++/3.4.2|D:/Dev-Cpp/include" "D:/Dev-Cpp/lib"
*/

var oWsh    = new ActiveXObject("WScript.Shell");
var oFso    = new ActiveXObject("Scripting.FileSystemObject");
var aArgs    = new Array();
var aSFiles    = new Array();
var aOFiles    = new Array();
var sTemp    = new String("");
var check    = 0;
var nIndex    = 0;
var nJndex    = 0;
var Re;
var oFile, oFolder, oFiles, oCP, oLP;
var aHeaders, aLibrars;

//----------------------------------------------------------------------------------------------------------
//    检查参数合法性
//----------------------------------------------------------------------------------------------------------

if(WScript.Arguments.length==8)
{
    try
    {
        sTemp    = WScript.Arguments(0);
        oFile    = oFso.GetFile(sTemp);
        oFile    = null;
        check++;
        aArgs["CompileBinPath"] = sTemp;

        sTemp    = WScript.Arguments(1);
        oFile    = oFso.GetFile(sTemp);
        oFile    = null;
        check++;
        aArgs["LinkerBinPath"] = sTemp;


        sTemp    = WScript.Arguments(2);
        oFolder    = oFso.GetFolder(sTemp);
        sTemp    = oFolder.Path + "//";
        oFolder = null;
        check++;
        aArgs["SocureDir"] = sTemp;

        sTemp    = WScript.Arguments(3);
        oFolder    = oFso.GetFolder(sTemp);
        sTemp    = oFolder.Path + "//";
        oFolder = null;
        check++;
        aArgs["OutputDir"] = sTemp;

        try
        {
            sTemp    = WScript.Arguments(4);
            if(sTemp.length<=0)
            {
                throw    "您所提供的目标程序文件名参数不正确,请提供正确的参数!";
            }
            else
            {
                Re    = //./;
                if(sTemp.search(Re)<0)
                {
                    sTemp    += ".exe";                    
                }
                aArgs["ExecFileName"] = sTemp;
                check++;
            }
            

            sTemp    = WScript.Arguments(5);
            if(sTemp.length<=0)
            {
                throw    "您所提供的库参数不正确,请提供正确的参数!";
            }
            else
            {
                aArgs["LinkerSwitch"] = sTemp;
                check++;
            }
        }
        catch(ErrInfo)
        {
            WScript.Echo(ErrInfo);
        }

        sTemp        = WScript.Arguments(6);
        aHeaders    = sTemp.split("|");
        if(aHeaders.length>0)
        {
            for(nIndex=0;nIndex<aHeaders.length;nIndex++)
            {
                oFolder    = oFso.GetFolder(aHeaders[nIndex]);
                oFolder    = null;
            }
        }
        else
        {
            oFolder    = oFso.GetFolder(sTemp);
            oFolder    = null;
            aHeaders    = new Array();
            aHeaders[0]    = sTemp;
        }
        check++;

        sTemp        = WScript.Arguments(7);
        aLibrars    = sTemp.split("|");
        if(aLibrars.length>0)
        {
            for(nIndex=0;nIndex<aLibrars.length;nIndex++)
            {
                oFolder    = oFso.GetFolder(aLibrars[nIndex]);
                oFolder    = null;
            }
        }
        else
        {
            oFolder    = oFso.GetFolder(sTemp);
            oFolder    = null;
            aLibrars    = new Array();
            aLibrars[0]    = sTemp;
        }
        check++;

    }
    catch(ErrObj)
    {
        if(ErrObj.number==-2146828235)
        {
            if(check==0)
            {
                WScript.Echo("您所提供的编译程序路径 [ " + sTemp + " ] 不正确!,请提供正确的参数!");
            }
            else if(check==1)
            {
                WScript.Echo("您所提供的连接程序路径 [ " + sTemp + " ] 不正确!,请提供正确的参数!");
            }
        }
        else if(ErrObj.number==-2146828212)
        {
            if(check==2)
            {
                WScript.Echo("您所提供的源文件所目录 [ " + sTemp + " ] 不正确!,请提供正确的参数!");
            }
            else if(check==3)
            {
                WScript.Echo("您所提供的输出目录 [ " + sTemp + " ] 不正确!,请提供正确的参数!");
            }
            else if(check==6)
            {
                WScript.Echo("您所提供的头文件目录表不正确!,请提供正确的目录!");
            }
            else if(check==7)
            {
                WScript.Echo("您所提供的库文件目录表不正确!,请提供正确的目录!");
            }
        }
        else
        {
            WScript.Echo("编译脚本在检测参数合法性时遇到不明的错误 [ " + ErrObj.number + " ],脚本将停止执行!");
        }
    }

    if(check==8)
    {
        //------------------------------------------------
        //    取得需要编译的源文件并生成其对应的编译目标文件名
        //------------------------------------------------
        oFolder = oFso.GetFolder(aArgs["SocureDir"]);
        oFiles    = new Enumerator(oFolder.files);
        nIndex    = 0;

        for(;!oFiles.atEnd();oFiles.moveNext())
        {
            sTemp    = oFiles.item().Path;
            if((sTemp.substr(sTemp.length-4,1)=="."&&sTemp.substr(sTemp.length-3,3)=="cpp")||(sTemp.substr(sTemp.length-2,1)=="."&&sTemp.substr(sTemp.length-1,1)=="c"))
            {
                aSFiles[nIndex] = sTemp;
                Re                = //./;
                sTemp            = oFiles.item().Name;
                nCheck            = sTemp.search(Re);
                aOFiles[nIndex] = aArgs["OutputDir"] + sTemp.substr(0,nCheck+1)+"o";            
                nIndex++;
            }
        }
        oFiles    = null;
        oFolder    = null;

        //------------------------------------------------
        //    编译源文件
        //------------------------------------------------
        WScript.Echo("---------- Compile ----------/r");
        check = 8;

        for(nIndex=0;nIndex<aSFiles.length;nIndex++)
        {
            sTemp    = aArgs["CompileBinPath"];
            sTemp    += " -c /"" + aSFiles[nIndex] + "/"";
            sTemp    += " -o /"" + aOFiles[nIndex] + "/"";
            for(nJndex=0;nJndex<aHeaders.length;nJndex++)
            {
                sTemp    += " -I/"" + aHeaders[nJndex] + "/"";
            }

            oCP        = oWsh.Exec(sTemp);
            sTemp    = "";
            while(oCP.status!=1)
            {
                if(!oCP.StdErr.AtEndOfStream)
                {
                    sTemp += oCP.StdErr.Read(1);
                }
            }
            if(sTemp.length==0)
            {
                WScript.Echo(aSFiles[nIndex] + " Compilation successful!/r");
                check++;
            }
            else
            {
                WScript.Echo(sTemp);
            }
            oCP        = null;
        }

        //------------------------------------------------
        //    连接目标文件
        //------------------------------------------------
        if(check==8+aSFiles.length)
        {
            WScript.Echo("/r---------- Linker ----------/r");
            
            sTemp = "";
            for(nIndex=0;nIndex<aOFiles.length;nIndex++)
            {
                sTemp    += " /"" + aOFiles[nIndex] + "/"";
            }
            sTemp    = aArgs["LinkerBinPath"] + " " + sTemp + " -o /"" + aArgs["OutputDir"] + aArgs["ExecFileName"] + "/"";
            for(nJndex=0;nJndex<aLibrars.length;nJndex++)
            {
                sTemp    += " -L/"" + aLibrars[nJndex] + "/"";
            }
            sTemp    += " " + aArgs["LinkerSwitch"];

            oLP        = oWsh.Exec(sTemp);
            sTemp    = "";
            while(oLP.status!=1)
            {
                if(!oLP.StdErr.AtEndOfStream)
                {
                    sTemp += oLP.StdErr.Read(1);
                }
            }
            if(sTemp.length==0)
            {
                WScript.Echo(aArgs["OutputDir"] + aArgs["ExecFileName"] + " Createing successful!/r");
                check++;
            }
            else
            {
                WScript.Echo(sTemp);
            }
            oLP        = null;
        }
    }

    //------------------------------------------------
    //    运行新生成的程序
    //------------------------------------------------
    if(check==8+1+aSFiles.length)
    {
        if(oWsh.Popup("代码已经成功编译及连接,是否要运行新生成的程序?",10,"运行程序",4|32)==6)
        {
            WScript.Echo("/r---------- Run ----------/r");
            
            check = oWsh.Run("/"" + aArgs["OutputDir"] + aArgs["ExecFileName"] + "/"",5,true);

            WScript.Echo("RturenVal( " + check + " ) ");
        }
    }
}
else if(WScript.Arguments.length<8)
{
    WScript.Echo("您提供的参数数量不够,脚本将停止执行!");
}
else if(WScript.Arguments.length>8)
{
    WScript.Echo("您提供的参数数量过多,脚本将停止执行!");
}

//
// 结束脚本执行
//
oWsh    = null;
oFso    = null;
aArgs    = null;
WScript.Quit();

 

 

 
上一篇:Koenig查找(ADL)简析  下一篇:论C/C++函数间动态内存的传递