将程序编译并运行到另一个程序时出错

时间:2017-05-12 22:00:38

标签: c linux

我想用c编写一个编译并运行另一个程序的程序。我写了这段代码并返回:

  

robi @ Robi:〜$ ./comp test.c

     

gcc:致命错误:没有输入文件

     

编译终止。

     

错误

我这样编译:

  

gcc -Wall -o comp Compilare.c

然后像这样运行:

  

./ comprun test.c

test.c就是这个

#include<stdio.h>

    int main(){
            printf("Ana are mere");
            return 0;
    }

代码:

#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>

int main(int argc,char* argv[]) {
        char comanda[100];
        strcpy(comanda,"gcc -o run");
        strcat(comanda,argv[1]);
        if(WEXITSTATUS(system(comanda) == 0))
                execl("./run","./run",NULL);
        printf("Error");
        return 0;
}

如何让它无故障运行?

1 个答案:

答案 0 :(得分:0)

问题是您在程序中调用的gcc命令如下所示:gcc -o runtest.c,即它尝试不编译任何输入到名为runtest.c的输出二进制文件。您需要在字符串文字末尾添加空格:"gcc -o run" - &gt; "gcc -o run "