8

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;
 }

Why could it be happening? All the call to this function is hello();

EDIT: The full error output:

 home.c: In function ‘hello’:
 hhme.c:838:7: error: variable ‘i’ set but not used [-Werror=unused-but-set-variable]
 home.c:840:1: error: ‘noreturn’ function does return [-Werror]
 cc1: all warnings being treated as errors
 make: *** [home.o] Error 1
Jay
  • 9,585
  • 6
  • 49
  • 72
user2073729
  • 1,151
  • 4
  • 10
  • 16

2 Answers2

29

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

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
4

Most probably, the function is marked with __attribute__((noreturn)). However, it does in fact return (when control reaches the end of irs body, since it doesn't enter an infinite loop, it doesn't call other "noreturn" functions, etc.)

I don't see what your point is in 1. marking the function as non-returning, 2. writing a function that does nothing - probably you could just eliminate both?

  • 3
    Clarifying this answer: for a function with __attribute__((noreturn)), if the function body does not end with an infinite loop, or a call to another noreturn function, you get this warning. In other words, the compiler does "flow analysis" and can determine whether the function actually might return. Remember there is an implicit return at the closing brace. – bootchk Apr 20 '17 at 19:46
  • 3
    @bootchk, +1 for "infinite loop". `f() { }` generates warning/error. `f() { while(1); }` is alright. Thanks – user3124812 May 11 '17 at 23:15