嵌入式warning fix

Tags: linux mips c

前言

 新人刚刚毕业入职,第一个任务是解决已有代码中的warning部分,一方面学习编程规范,另一方面清理了既有代码中的warning之后,以后的编译命令可以更加严格,使得项目编程更加规范.在CSDN上我找到两篇关于warning的文章,再加上我自己的部分,刚好做个总结.
(编译器是mips-linux-gcc)

  • 1.warning:implicit declaration of function 'XXX'
    原因分析:
    1,没有把函数所在的c文件生成.o目标文件;
    2,在函数所在的c文件中定义了,但是没有在与之相关联的.h文件中声明;
    3,使用的函数为系统库自带的函数;
    解决方法:
    1,在使用函数的c文件前,使用extern关键字声明该函数;
    2,在函数声明添加到与之关联的.h文件中;
    3,在使用函数的c文件前,声明自带的系统库头文件;

  • 2.warning: long int format, unsigned int arg (arg 4)
    原因分析:
    printf函数的占位符为%ld,参数为unsigned int,不匹配;
    解决方法:
    1,可以修改占位符为%u;
    2,也可以强制转化无符号整型的格式为长整型;

  • 3.warning: assignment from incompatible pointer type
    原因分析:字面分析是指针类型不匹配;
    解决方法:类似于上面,将数据类型修改为相同;

  • 4.warning: ‘XXX’ declared `static’ but never defined
    原因分析:声明为static的函数不能被其他文件所使用,如果其他文件使用,那么就会报上面的错误。
    解决办法:将函数的声明从.h中移到.c文件中;

  • 5.warning: passing arg 4 of `XXX’ makes integer from pointer without a cast
    原因分析:数据类型不匹配;
    解决方法:具体问题具体分析,主要还是要将数据的类型统一;

  • 6.warning: `return’ with no value, in function returning non-void
    原因分析:函数执行完成后没有返回值,同时函数的定义中有返回值;
    解决方法:通常建议在函数的末尾按照函数的定义添加返回值;

  • 7.warning: unused variable `XXX’
    原因分析:在堆栈中分配了内存空间,没有使用;
    解决方法:可以删掉,我个人感觉还是注释掉比较好,不一定什么时候就想起来要用了;


  • 9.warning: initialization makes integer from pointer without a cast
    原因分析:定义的变量类型和初始化类型不符
    解决方法:具体问题具体分析,主要还是要将数据的类型统一;

  • 10,error: previous declaration of ‘rut_startRipd’ was here
    原因分析:命名冲突
    解决方法:多人分工的大型项目容易发生命名冲突,修改下函数名即可,注意这里要修改相关的所有位置的函数名;

  • 11,warning: ‘XXX’ defined but not used
    原因分析:定义的函数未被使用
    解决方法:可以删掉,我个人感觉还是注释掉比较好,不一定什么时候就想起来要用了;

个人总结:这个项目中的大部分warning都是对数据类型不够敏感发生的;在自己今后的编程中不要反正这样的问题了;

附上网上找到的其他warning,供参考:
一些warning(编译器是mips-linux-gcc)

1,warning: “/*” within comment
举例: /**************************************/
/*
/* save snmp entry data
/* add by Tina Lee 2003/7/11
/***************************************/
说明:意思是说/* / 中间又包含了/
修改:改成这样就好了
/**************************************
*
* save snmp entry data
* add by Tina Lee 2003/7/11
***************************************/

2,warning: no previous prototype for ‘get_char_for_sta’
举例:无
说明:函数没有声明,只有定义
修改:在相应的.h文件中添加该函数的声明。

3,warning: unused parameter ‘mcb’
举例:
int ifnMenuQuit(MCB_T *mcb)
{
return QUIT;
}
说明:因为函数参数中的mcb,在该函数中没有被使用,所以产生warning
修改:对没使用的参数使用 para=para;
int ifnMenuQuit(MCB_T *mcb)
{
mcb=mcb; <———-添加该行
return QUIT;
}

4,warning: comparison between signed and unsigned
举例:
INT4 s4UnitID = 0;
INT4 s4ChipID = 0;
uint32 u0 = 0;
PMAP_BUNIT_ITE (s4UnitID, u0, s4ChipID)
说明:类型不一致。
修改:使用相同的类型(视具体情况而定)。

5,warning: unused variable `iRet’
举例:
func()
{
int iRet=error_none;
……………
……………
return error_none;
}
说明:函数中定义局部变量iRet,但没有使用。
修改:(1)删除该变量
(2)在合适的地方使用该变量
如结尾改为: return iRet;

6,warning: suggest parentheses around assignment used as truth value
举例:
func(char *format)
{
char c;
while(c=*format++)
{
………….
}
}
说明:该warning的意思是建议程序员对while中的条件语句加上括号,因为编译器不知道到底是
=,还是==
修改:while((c=*format++))
明确告诉编译器,这里确实是赋值语句,然后判断c是否为真。

7,warning: declaration of ‘remove’ shadows a global declaration
举例:
int
bcm_port_learn_modify(int unit, bcm_port_t port, uint32 add,
uint32 remove)
{
int rv;
PORT_PARAM_CHECK(unit, port);
PORT_LOCK(unit);
rv = _bcm_port_learn_modify(unit, port, add, sdkremove);
PORT_UNLOCK(unit);
return rv;
}
说明:因为库函数stdio.h中使用了全局变量remove,所以和该函数声明中的remove冲突。
修改:把该函数的变量名改掉。如把remove 改为 sdkremove
附 : linux 的patch中也是采用的修改变量名的方法。
linux patch

8,warning: redundant redeclaration of ‘ifnDispTitle’
举例:在m_main.c中第50行 int ifnDispTitle(MCB_T *mcb);
在menuext.h中第954行 extern int ifnDispTitle(MCB_T *mcb);
说明:产生这种warning多数情况是因为m_main.c没有对于的.h文件,因此该函数在.c文件中声明,所以
在别的地方用该函数的时候,使用 extern funcname()来声明,就会产生这种warning.
解决方法:还没想到

9,warning: missing braces around initializer
举例:typedef strunc tS{
int a;
int b;
int c;
}S;
S s[3]={
1,2,3,
4,5,6,
7,8,9
};
说明:这个warning是说同一个结构体中的数据初始化的时候应该放在一个括号里面。在menu结构体初始化
中,有大量的此类warning,加上括号即可解决。
修改:加上括号即可。
S s[3]={
{1,2,3},
{4,5,6},
{7,8,9}
};

10,warning: function declaration isn’t a prototype
举例:在mac_if.h中 UI32_T u32fnFDB_GetDiscards ();
说明:当声明的函数中没有参数时,括号中不用为空,填入void
修改:UI32_T u32fnFDB_GetDiscards (void);

11,suggest explicit braces to avoid ambiguous `else’
举例:M_A_MIRO.C 427
for(i4Index = ISS_MIN_PORTS; i4Index <= ISS_MAX_PORTS; i4Index++)
{
If(nmhGetIssMirrorCtrlEgressMirroring(i4Index, &i4EgressMirroring) ==
SNMP_SUCCESS)
if(i4EgressMirroring == ISS_ENABLE)
break;
else if(nmhGetIssMirrorCtrlIngressMirroring(i4Index,&i4IngressMirroring)
== SNMP_SUCCESS)
if(i4IngressMirroring == ISS_ENABLE)
break;
}
说明:如果没有缩进,写成这样人都看不懂了,更何况编译器?
修改:重写这段代码。
附: 在web的diffserv部分,有一部分代码使用
if
;
else if
;
……..
else
;
其嵌套达到10层以上,令人叹为观止,结果编译出来的代码有问题,产生error.

12,warning: comparison between signed and unsigned
举例:int iLen
iLen = strlen(au8Buffer);
if (iLen == strlen(au8Buffer1) && strncmp(au8Buffer, au8Buffer1, iLen)
== 0)
……………..;
……………..;
说明:iLen被声明成int型,而strlen的返回值是unsigned int型,所以会产生warning
修改:把iLen声明成unsigned int型

13,warning:array subscript has type `char’
举例: I8_T i8TrunkSearch;
if(i32TrunkID[i8TrunkSearch]==i32CurrentTrunk)
……………;
……………;
说明:这个warning是说,数组的下标被定义成char型了,由于char型有可能是负数,因此会产生难以
预料的错误。
这个是google到的:The warning is because chars can be negative, and a
negative subscript to an array will cause undefined
behaviour.This is not a required diagnostic; I guess
the GCC developers feel that this error is more likely
to occur with a char than with other signed integral
types。
修改:使用无符号类型替代有符号类型。

14:warning: `return’ with no value, in function returning non-void
举例:
INT1 MSTPGetInstanceVlanMap(UI32_T u32InstIndex,UINT1 *vlanlist)
{
……………..;
……………..;
VlanMap = (tSNMP_OCTET_STRING_TYPE*)
allocmem_octetstring(CLI_MSTP_MAX_VLANMAP_BUFFER)
if (VlanMap == NULL)
{
return;
}
……………;
……………;
}
说明:由于该函数要求返回一个INT1型的数,而这里使用return;所以会产生warning
修改:添加上相应的返回值。

15,warning: control reaches end of non-void function
举例:
int vfnDot1xPortInit(MCB_T *mcb)
{
UINT4 u4Interface;
for(u4Interface=1;u4Interface

原文地址:https://www.cnblogs.com/chengyi818/p/5094444.html