今天在查阅u-boot代码时,发现一个奇怪的地方。记录下来,以备后续追溯。
u-boot代码的main.c模块中,有一个cread_line函数,其中有一段话是这样的:
case '\t':
int num2, col;
/* do not autocomplete when in the middle */
if (num < eol_num) {
getcmd_cbeep();
break;
}
buf[num] = '\0';
col = strlen(prompt) + eol_num;
num2 = num;
if (cmd_auto_complete(prompt, buf, &num2, &col)) {
col = num2 - num;
num += col;
eol_num += col;
}
break;
编译时报错:
main.c: In function 'cread_line':
main.c:863: error: a label can only be part of a statement and a declaration is not a statement
main.c:891: error: a label can only be part of a statement and a declaration is not a statement
make[1]: *** [main.o] 错误 1
此块代码不能再声明变量,如果你声明变量就会报错,除非用括号括起来,这是编译器的问题,也可以说是C语言的规定。写代码的时候注意就成了。就像C语言再声明的时候不能赋值一样。
所以,最简单的解决办法就是,将case中的代码用括号括起来:
case '\t':
{
int num2, col;
/* do not autocomplete when in the middle */
if (num < eol_num) {
getcmd_cbeep();
break;
}
buf[num] = '\0';
col = strlen(prompt) + eol_num;
num2 = num;
if (cmd_auto_complete(prompt, buf, &num2, &col)) {
col = num2 - num;
num += col;
eol_num += col;
}
break;
搞定!收工!