c-使用popen编译外部程序



我正在创建一个在线评委。为此,我正在编写一个CGI c脚本,它获取程序内容并编译程序。

这是我编译一个外部c程序的函数。

char *compile_program(char *compile_script){
printf("%sn", compile_script);
compile_script = "gcc /var/www/FilebCamFz.c -o /var/www/FilebCamFz";
FILE *output_file;
output_file = popen(compile_script, "w");
char *output, *full_output, *full_error;
output = malloc(50000);
full_output = malloc(50000);
full_error = malloc(50000);
setbuf(stderr, full_error);
setbuf(stdout, full_output);
if(output_file == NULL){
    // Some error
    fprintf(stderr, "Compilation failed. Try again.n");
    return full_error;
}
else{
    // If command executed successfully
    while (fgets(output, 5000000, output_file) != NULL){
       strcat(full_output, output);
    }
    if (pclose (output_file) == 0){
        return full_output;
    }
    else
        fprintf (stderr, "Could not run more or other error.n");
        return full_error;
}
//return EXIT_SUCCESS; }

当使用lsfree -m命令调用函数时,输出如预期。

但是,当运行python -v等命令时,会获得空输出。

在运行像gcc /var/www/FilebCamFz.c -o /var/www/FilebCamFz这样的命令时,我在pclose()上得到了一个stderr。

所以我的问题是,是什么原因导致了popen这种不寻常的行为。以及如何通过popen() 编译外部程序(c、c++、python、java等)

我们最终通过使用docker容器解决了这个问题。

最新更新