Search code examples
c++gccg++exit-code

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


When doing the following, the exit code of the failed compilation is 1, however, based on this, I would expect it to be >=3. Why is this? What can I do if I want more detailed exit codes than binary 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)

Thanks.


Solution

  • The documentation that you point at belongs to gcc version 3.3.6. However, in version >=4, the documentation for that compiler flag has changed and it now only states:

    -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.

    Which is a little confusing when reading it. It does not specify the error codes that were well explained in the documentation of 3.x. Then, it is possible that the exit code that you are looking for is not generated any more in version 4.x of gcc/g++.