'不返回的“功能不会返回 [英] ‘noreturn’ function does return

查看:1354
本文介绍了'不返回的“功能不会返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译下面的C程序中,我得到这样的警告:
'不返回的'函数不返回。这是在功能:

 无效打招呼(无效){
  INT I;
  I = 1;
 }

为什么它会是这样吗?
所有调用该函数是你好();

编辑:
完整的错误输出:

  home.c:在函数'你好':
 hhme.c:838:7:错误:变量'我'设置,但不使用[-Werror =未使用,但设置变量]
 home.c:840:1:错误:'不返回的功能不会返回[-Werror]
 CC1:所有的警告被视为错误
 使:*** [home.o]错误1


解决方案

有可能告诉 GCC 一个特定函数永远不会返回。这使得某些优化并有助于避免未初始化变量的假警告。

这是使用<$c$c>noreturn属性:

 无效FUNC()__attribute__((noreturn))的;

如果函数尽管不返回的属性返回,编译器,你看到(在你的情况下,被转换成一个错误),警告。

既然你不可能使用不返回的在code时,可能的解释是,你有他的名字冲突与标准<$ C函数$ C>不返回的的功能,如下面的例子:

 的#include&LT;&stdlib.h中GT;无效退出(INT){
} //警告:'不返回的功能不会返回[默认启用]

在这里,我的退出冲突与 出口(3)

对于这种冲突的另一个明显的候选人是 中止(3)

当然,如果你的函数实际上是调用你好(),罪魁祸首是几乎可以肯定的某处code群中。

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

解决方案

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.

这篇关于'不返回的“功能不会返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆