属于自己的【在线编译工具】

本文灵感来自一个想偷懒的程序员,在验证一些想法而不得不创建一个vs项目或者是费力的安装g++编译器时所使用的在线编译器

(附上网址http://www.dooccn.com/cpp/

某天发现腾讯云服务器打折(还是打骨折)就大方的花几十块钱买了一台1g内存双核Cpu的服务器,在上面用开源的TinyWebServer搭建了一个web服务器。
在我苦思冥想能给这个孤零零的网站上面搞点什么东西的时候,在线编译器是怎么嫩出来的这么一个有趣的想法诞生了。

不废话,下面是我自己的思路(C++在线编译版):
1.在linux服务器上安装g++编译器(有这玩意就能编译C++代码了)
2.通过自己搭建的TinyWebserver在网页端上传代码(文本),web服务器将其保存为xxx.cpp文件
3.web服务器调用用linux系统指令 "g++ -Wall xxx.cpp -o xxx &>%scompileInfo_%s.html" 编译生成可执行文件,并将编译结果写入一个html文件里
如果编译报错就将该html文件发给前端显示,编译成功就执行,返回执行结果给前端显示

int Handler(char const  *name, char *data, char * targetFile)
    {
        if(name == NULL || data == NULL)
        {
            printf("Handler no name or data");
            return 1001;
        }
        printf("Compliler...... : %s \n", name);
        const char* path = "./projects/CodeOnline-Cpp/RunTime/";
        const char* webFilePath = "./root/pro_compile/";
        string oriStr(data);
        string decodeStr = UrlDecode(oriStr);

        ofstream outfile;
        char fileName[256];
        //memset(fileName, 0, sizeof(fileName));
        //char fileName[128];
        sprintf(fileName, "%s%s.cpp", path,name);
        outfile.open(fileName);
        outfile << decodeStr.c_str() << endl;
        outfile.close();
    
        char cmd[2048];
        //memset(cmd, 0, sizeof(cmd));
        //编译
        sprintf(cmd, "g++ -Wall %s%s.cpp -o %s%s &>%scompileInfo_%s.html",path, name,path, name, webFilePath,name);
    
        printf("cmd : %s", cmd);
        int status = system(cmd);
        if (status == -1)
            printf("system error");
        else
        {
            if (WIFEXITED(status))
            {
                int sub = WEXITSTATUS(status);
                if (sub == 0) //子进程正常退出 => 编译成功
                {
                    printf(" \n======>  Compile successful  !!!\n");
    
                    //saveFile(data);

                    char cmdexe[2048];
                    sprintf(cmd, "%s%s  > %sOut%s.html\n", path,name, webFilePath, name);
                    int statusExe = system(cmd);
                    //printf("\nstatusExe : %d", statusExe);

                    if (statusExe == -1)
                        printf("system error");
                    else
                    {
                        if (WIFEXITED(statusExe))
                        {
                            int subExe = WEXITSTATUS(statusExe);
                            if (subExe == 0) //子进程正常退出 => 编译成功
                            {
                                printf(" \n======>  Run successful  !!!\n");

                                char cmdexeRet[2048];

                                sprintf(cmdexeRet, "/pro_compile/Out%s.html", name);
                                strcpy(targetFile, cmdexeRet);
                                printf("\nRUN DONE!!!!\n");
                            }
                        }
                    }



                    return 0;
                }
                else if (sub == 1) //编译失败
                {
                    char cmdexeRet[2048];

                    sprintf(cmdexeRet, "/pro_compile/compileInfo_%s.html",name);
                    strcpy(targetFile, cmdexeRet);
                    printf("\ncomplie fail!!!!\n");
                    return 1;
                }
    
                else
                {
                    printf("run command fail and exit code is %d\n", WEXITSTATUS(status));
                    return 2;
                }
            }
            else
            {
                printf("exit status = %d\n", WEXITSTATUS(status));
                return 3;
            }
        }
        return 1000;
    }
    

};

成果:在线编译

猜你喜欢

转载自blog.csdn.net/Yang9325/article/details/122382559
今日推荐