‘noreturn’ function does return

前端 未结 2 1672
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 02:53

When I compile the C program below, I get this warning: ‘noreturn’ function does return. This is the function:

void hello(void){
  int i;
  i=1;         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-12 03:18

    It is possible to tell gcc that a particular function never returns. This permits certain optimizations and helps avoid spurious warnings of uninitialized variables.

    This is done using the noreturn attribute:

    void func() __attribute__ ((noreturn));
    

    If the function does return despite the noreturn attribute, the compiler emits the warning you're seeing (which in your case gets converted into an error).

    Since you're unlikely to be using noreturn in your code, the likely explanation is that you have a function whose name clashes with a standard noreturn function, as in the below example:

    #include 
    
    void exit(int) {
    }                // warning: 'noreturn' function does return [enabled by default]
    

    Here, my exit clashes with exit(3).

    Another obvious candidate for such a clash is abort(3).

    Of course, if your function is actually called hello(), the culprit is almost certainly somewhere within your code base.

提交回复
热议问题