Eliminate compile warning)

Source: Internet
Author: User

How to view compilation warnings

When the Compilation Program finds that there is a problem somewhere in the program, it may give a warning message. Warning Information may indicate a large hidden error in the program, or it may be true. The correct way to handle the warning should be: eliminate it as much as possible. Every warning given by the compiled program should be carefully analyzed to see if there is a problem. Only warnings with no problems can be put down.

Note:

Due to various compilation warnings, they cannot be listed one by one. Below are some typical warnings and some warnings. You only need to give them literally, you can quickly find and solve the problem.

 

 

Type 1:

Display:Warning: Implicit declaration of function 'example ()'.

Warning cause: (1) the example () function is called in your. c file, but you have not included the corresponding. h file that declares this function.

(2) You may have defined this function body in a. c file, but it is not declared in. h.

Solution: (1) Add extern example () at the beginning of the. c file that calls this function ();

(2) You can include the header file that declares the function example () in the. c file that calls this function.

(3) If you are in. the C file defines this function body, but it is not in. h. h file, add your function declaration.

Similar warning:Warning: Type mismatch with previous implicit Declaration

Warning: Type mismatch with previous implicit Declaration

Warning: Previous implicit declaration of 'example ()'

Type 2:

Display:Warning: unused variable 'param '.

Warning cause: Obviously, you have defined the variable 'param', but you have not used it at all.

Solution: delete it if it is not needed.

 

Type 3:

Display:Warning: statement with no effect.

Warning cause: It is possible that you do this in your file -- # define macroprint

Then -- # define macroprint printf is defined in one place. Then you reference

Macroprint ("hello"), which does not cause errors, but a warning is reported, "This statement is useless ".

Solution: delete # define macroprint.

 

Type 4:

Display:Warning: int format, long int Arg (ARG 3)

Warning cause: Like this printf ("% S % d, szdebugstring, ulgwid); your ulgwid is of the unsigned long type, the output format you select for it is "% d" (This format is an integer-int service ).

Solution: for this error, you only need to make the parameter type consistent. As shown above, you only need to change "% d" to "% lD.

Similar warning:Warning: Comparison between pointer and integer

Type 5:

Display:Warning: comparison is always 0 due to limited range of data type

Warning cause: You may have defined the unsigned int uparam, but you have made the IF (uparam <0) judgment,

Because the unsigned int type data is always greater than or equal to 0, this comparison provides a warning because the data type limits its range.

Solution: Remove this judgment.

 

Type 6:

Display:Warning: control reaches end of non-void Function

Warning cause: You may have written a warning

Unsigned long funca ()

{

If ()

{Return ulvalue ;}

If ()

{Return ulvalue ;}

} Function, maybe in two if statements, you did not enter. At this time, before exiting the function, you have no value to return.

Solution: If a function has a return value, make sure that the function has a return value under any circumstances.

Similar warning:Warning: 'Return 'with no value, in function returning non-void

Type 7:

Display:Warning: overflow in implicit constant conversion

Warning cause: the variable transformation may cause the value to be out of bounds.

# Define ret_productid 0x10000000

# Define err_ret_global ret_productid + 5000

# Define ret_user err_ret_global ++ 5000

# Define user_ OK ret_user + 0

# Define user_fail ret_user + 1

If this definition is used, if the short func () {return user_ OK} is encountered, an out-of-bounds warning will be triggered.

Solution: determine the value range.

 

Type 8:

Display:Warning: 'ulparam' might be used uninitialized in this function

Warning cause: When ulparam is used as the right value of the expression, you have not initialized this parameter before.

Example: void func ()

{

Ulong ulparam;

Ulong ulretcode;

If (...)

{

Ulparam = ........;

}

If (....)

{

Ulparam = ......;

}

Ulretcode = ulparam;

}

In this case, when neither if () can be executed, ulparam is not assigned a value at all. In this case, it is more dangerous to assign a value to ulretcode.

Solution: leave one more God and be careful.

 

Type 9:

Display:Warning: Passing Arg 1 of 'free' makes pointer from integer without a cast

Warning cause: You are free (a), but a is an unsigned long. You may put the value of a pointer in.

Solution: When free (a) is used, you must forcibly convert a to the pointer type. That is, free (char *) ).

Similar warning:Warning: assignment from incompatible pointer type

Warning: initialization from incompatible pointer type

Warning: Passing Arg 2 of 'aos _ memcopy_x 'makes pointer from integer without a cast

 

Type 10:

Display:Warning: 'My _ debug' redefined

Warning: this is the location of the previous definition

Warning cause: two consecutive warnings may occur: You include two. h In Your. c file.

File, and both. H files are declared on my_debug.

Solution: declare such stuff in only one file.

 

Type 11:

Display:Warning: Value computed is not used

Warning cause: the value involved in the operation does not work. For example:

Char * P;

* P ++;

This has no effect on P at all.

Solution: Determine the operation to be performed.

 

Type 12:

Display:Warning: '# ifdef' Argument starts with a digit

Warning cause: An error such as # ifdef 0 occurs.

Solution: it should be # If 0

 

Type 13:

Display:Warning: Unknown escape sequence '\ R'

Warning cause: the compiler does not know '\ R '.

Solution: Click it by mistake. It should be '\ R '.

 

Type 14:

Display:Warning: Too few arguments for format

Warning cause: You may have done this: printf ("% d % s", uparam );

Solution: Leave what you want and do not remove it.

Type 15:

Display:Warning: 'func' defined but not used

Warning cause: You defined the func function, but you didn't use it at all.

Solution: Remove unnecessary items.

 

Type 16:

Display:Warning: Suggest parentheses around & within |

Warning cause: Some people use if (* P> = 'A') & (* P <= 'Z') | (* P> = 'A ') & (* P <= 'Z') | (* P> = '0') & (* P <= '9 '))

Solution: If (* P> = 'A') & (* P <= 'Z ')) | (* P> = 'A') & (* P <= 'Z') | (* P> = '0 ') & (* P <= '9 ')))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.