我使用 execvp 编译有错误的程序。但是随后我的终端屏幕上会弹出错误消息,这不应该发生,因为如果 execvp 失败,它只会让孩子以退出状态返回。我不明白为什么我的终端实际上会显示错误消息?
我的命令数组:{gcc,studentcode.c,ourtest3.c,-o,ourtest3.x,NULL} 在 ourtest3.c 中,我故意犯了几个错误。我的调用函数是这样的:
commands = {"gcc", "studentcode.c", "ourtest3.c", "-o", "ourtest3.x", NULL};
int compile_program(Char** commands) {
pid_t pid;
int status;
pid = safe_fork();
if (pid == 0) { /*Child*/
if (execvp(commands[0], commands) < 0) {
exit(0);
}
} else { /*Parent*/
if (wait(&status) == -1) {
return 0;
}
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0) {
return 1;
}
} else {
return 0;
}
}
return 1;
}
ourtest3.c 是这样的:
#include <stdio.h>
#include <assert.h>
#include "studentcode.h"
int main(void) {
assert(product(2, 16) == 32
printf("The student code in public07.studentcode.c works on its ");
printf("third test!\n");
return 0;
}
我的程序应该以返回值 0 正常结束,但是在我的终端窗口上,它显示
ourtest3.c: In function 'main':
ourtest3.c:19:0: error: unterminated argument list invoking macro "assert"
ourtest3.c:13:3: error: 'assert' undeclared (first use in this function)
ourtest3.c:13:3: note: each undeclared identifier is reported only once for each function it appears in
ourtest3.c:13:3: error: expected ';' at end of input
ourtest3.c:13:3: error: expected declaration or statement at end of input