0
votes

I want to make a program in c that compiles and runs another program. I wrote this code and it returns this:

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

gcc: fatal error: no input files

compilation terminated.

Error

I compile it like this:

gcc -Wall -o comp Compilare.c

and run it like this:

./comprun test.c

test.c is this

#include<stdio.h>

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

The code:

#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;
}

How can I make it run without errors?

1
try adding a space after run in strcpy - Erik W
Use a debugger and/or debug print statements to examine the program as it executes. You should be able to find the problem easily by yourself if you did even very basic debugging. - kaylum
You are running gcc -o runtest.c instead of gcc -o run test.c. Just add the space and it should be fine. - that other guy
along with the problems exposed in the other comments, this line: printf("Ana are mere"); will only output to the stdout buffer, You want output through that buffer to the display, so the line should be: printf("Ana are mere\n"); or printf("Ana are mere"); fflush( stdout ); - user3629249

1 Answers

0
votes

The issue is that the gcc command you invoke inside the program looks like this: gcc -o runtest.c, i.e., it attempts to compile no input to an output binary called runtest.c. You need a space at the end of your string literal: "gcc -o run" -> "gcc -o run "