读书人

IAR中不能用goto?该如何处理

发布时间: 2012-08-21 13:00:21 作者: rapoo

IAR中不能用goto?

首先,不需要提:最好不要用goto的忠告了,这个不是重点,我现在是要用goto的




我在main中有:


GSM:GSM_Reset();


在 timer.c中有语句:
if(voice_delayed>0)
{
voice_delayed--;
goto GSM;
}

头文件包含的顺序是也是先#include "main.h"后#include "timer.h"



编译的时候报错:
Warning[Pe177]: label "GSM" was declared but never referenced

连接的时候报错:
Error[Pe114]: label "GSM" was referenced but not defined


我定义了的,为何不还说没定义?

在IAR帮助文件中找到:

Control flow

……

Rule 14.4 (required)The goto statement shall not be used.
How the rule is checkedThe compiler will generate an error, indicating a violation of this rule, if a goto statement is used.


goto 不能用?

我这样做,

int hour=0;
while(1)
{
if( hour==1)
goto GSM;

……

if(hour==1)
{

input_fp_chck();
GSM:GSM_Reset();
}

……
hour=1;
}

编译通过,连接通过,运行OK.

什么情况? goto 不能跨文件吗?

[解决办法]
goto 在文件域内跳转
[解决办法]
goto的作用域是在函数内部的,想要跨函数,甚至是文件的话,可以试试信号的方式。
[解决办法]
C语言中goto可以跨函数的,除非标号有问题。

读书人网 >单片机

热点推荐