C/C++:error: return-statement with a value, in function returning ‘void’ [-fpermissive]

示例代码

#include <stdio.h>
void fun()
{
    
    
        printf("abc");
        return 1;
}
int main()
{
    
    
        fun();
}

gcc 编译,按照C的标准

提示警告;不是错误;在需要void返回类型时,却给了个int类型的返回值。
return.c: In function ‘fun’:
return.c:5:9: warning: ‘return’ with a value, in function returning void
return 1;
^
return.c:2:6: note: declared here
void fun()

g++, 按照C++

就显示错误;如果要消除这个错误,可以使用fpermissive,将错误降级成警告。
return.c: In function ‘void fun()’:
return.c:5:9: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
return 1;
^

-fpermissive

这个解释有几个名词需要理解;
降级:
检查:
一致;conformant;这里的一致性指的是是否符合标准。符合ABI的定义。
不一致:nonconformant;如果不一致,可能导致未知、未定义的错误。

Downgrade some diagnostics about nonconformant code from errors to warnings.
Thus, using ‘-fpermissive’ allows some nonconforming code to compile.

所以最终的解释是,加上这个选项可以允许编译通过,但是有机率出现未知、未定义错误。

猜你喜欢

转载自blog.csdn.net/qq_36428903/article/details/125326246