使用popen编译外部程序

时间:2016-01-03 05:55:50

标签: c linux popen

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

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

char *compile_program(char *compile_script){
printf("%s\n", 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等)

1 个答案:

答案 0 :(得分:0)

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

相关问题