我需要用系統編譯一個程序,我有存檔的名字和執行文件的名字,子執行gcc -o file.c exe1,父執行./exe1例如用C編譯系統
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
if (argc != 3) {
printf("Debe ingresar el nombre del archivo a compilar y el ejecutable");
exit(1);
} else {
char *archivo1 = strcat("gcc ", argv[1]);
char *archivo2 = strcat(archivo1, ".c -o ");
char *archivo3 = strcat(archivo2, argv[2]);
char *ejecutable = strcat("./", argv[2]);
pid_t pid_hijo;
int valor;
switch(pid_hijo = fork()) {
case -1:
printf("No se pudo crear el hijo");
case 0:
valor = system(archivo3);
default:
wait(NULL);
valor = system(ejecutable);
}
}
return 0;
}
你'case'塊應該有'breaks'。 –
看看sprintf()而不是strcat – Jack