在 g++ 中使用 -pass-exit-codes 时出现意外退出状态

Unexpected exit status when using -pass-exit-codes in g++

执行以下操作时,编译失败的退出代码为 1,但是,基于 this,我预计它会 >=3。为什么是这样?如果我想要比二进制更详细的退出代码怎么办 success/fail?

> echo "int main() {fail}" > fail.cpp
> g++ -pass-exit-codes fail.cpp -o fail
fail.cpp: In function ‘int main()’:
fail.cpp:1: error: ‘fail’ was not declared in this scope
fail.cpp:1: error: expected ';' before ‘}’ token
> echo $?
1
> g++ --version
g++ (GCC) 4.1.3 20080704 (Red Hat 4.1.2-27)

谢谢。

您所指向的文档属于 gcc 版本 3.3.6。但是,在版本 >=4 中,该编译器标志的文档已更改,现在仅声明:

-pass-exit-codes
Normally the gcc program exits with the code of 1 if any phase of the compiler returns a non-success return code. If you specify -pass-exit-codes, the gcc program instead returns with the numerically highest error produced by any phase returning an error indication. The C, C++, and Fortran front ends return 4 if an internal compiler error is encountered.

读起来有点乱。它没有指定 3.x 的文档中很好解释的错误代码。那么,您正在寻找的退出代码可能在 gcc/g++ 的 4.x 版本中不再生成。