【发布时间】:2017-05-12 22:00:37
【问题描述】:
我想用 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;
}
我怎样才能让它正常运行?
【问题讨论】:
-
尝试在
strcpy中的run之后添加一个空格 -
使用调试器和/或调试打印语句在程序执行时检查程序。如果你做了非常基本的调试,你应该可以自己轻松找到问题。
-
您正在运行
gcc -o runtest.c而不是gcc -o run test.c。加个空格就可以了。 -
连同其他 cmets 中暴露的问题,这一行:
printf("Ana are mere");只会输出到stdout缓冲区,您希望通过该缓冲区输出到显示器,所以该行应该是:printf("Ana are mere\n");或printf("Ana are mere"); fflush( stdout );