现在,我正在开发一个项目,在该项目中我从命令行自动编译 .c 文件。为此,我使用以下代码:
bool auto_compile(process_t *p) {
char* file_name = p->argv[0];
char* compile_args[] = {"gcc", "-o", "devil", file_name, NULL};
int status, pid;
switch(pid = fork()) {
case -1:
logger("fork", 1);
exit(EXIT_FAILURE);
case 0:
execvp("gcc", compile_args);
default:
waitpid(pid, &status, WUNTRACED);
if(WIFEXITED(status)) {
if(status != EXIT_SUCCESS) {
logger("Error, could not compile", 4);
return false;
}
}
}
return true;
}
当我为 gcc 输入无效参数(即无效文件名)时,它会将 gcc 错误消息打印到终端。如果我想将此错误重定向到文件,我将如何捕获它?