Own [online compilation tool]

This article is inspired by a programmer who wants to be lazy, and has to create a vs project when verifying some ideas or the online compiler used when installing the g++ compiler laboriously

(Attach URL http://www.dooccn.com/cpp/ )

One day, I found out that the Tencent Cloud server was discounted (it was still fractured), so I generously spent tens of dollars to buy a 1g memory dual-core CPU server, and built a web server on it with the open source TinyWebServer.
When I was thinking hard about what I could do for this lonely website, an interesting idea of ​​how the online compiler came out was born.

No nonsense, the following is my own idea (C++ online compilation version):
1. Install the g++ compiler on the linux server (you can compile C++ code with this thing)
2. Upload the code on the web page through the TinyWebserver built by yourself ( text), the web server saves it as xxx.cpp file
3. The web server invokes the linux system command "g++ -Wall xxx.cpp -o xxx &>%scompileInfo_%s.html" to compile and generate an executable file, and compile The result is written into an html file.
If an error is reported during compilation, the html file will be sent to the front-end for display. If the compilation is successful, it will be executed, and the execution result will be returned to the front-end for display.

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;
    }
    

};

Result: Online Compilation

Guess you like

Origin blog.csdn.net/Yang9325/article/details/122382559