【问题标题】:Error when compiling and running a program into another program将程序编译并运行到另一个程序时出错
【发布时间】: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 );

标签: c linux


【解决方案1】:

问题是您在程序中调用的gcc 命令看起来像这样:gcc -o runtest.c,即它不尝试将输入编译为名为@9​​87654323@ 的输出二进制文件。您需要在字符串文字的末尾有一个空格:"gcc -o run" -> "gcc -o run "

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 2011-10-31
    相关资源
    最近更新 更多