简体   繁体   中英

Error when compiling and running a program into another program

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?

The issue is that the gcc command you invoke inside the program looks like this: gcc -o runtest.c , ie, 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 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM