GCC common errors

Source: Internet
Author: User
Tags integer numbers parse error

From: http://wenku.baidu.com/view/01f99435f111f18583d05a47.html

1
GCC common errors
I. Error Type
Class 1: C syntax error
Error message: line N of the file source. C contains a syntax error (Syntex errror ).
Errors of this type are generally caused by C syntax errors. Check the source code file carefully.
The program before line N and line N sometimes needs to check the header files contained in the file.
In some cases, a very simple syntax error occurs. GCC will give a lot of errors, so keep the errors clear at this time.
Don't be intimidated by your waking mind. If necessary, refer to the basic C-language teaching materials.
Category 2: header file Error
Error message: the header file head. H (can not find include file head. h) cannot be found ).
This type of error is caused by the header file inclusion in the source code file, which may be caused by a wrong header file name,
The directory name of the specified header file is incorrect, or double quotation marks and angle brackets may be incorrectly used.
Category 3: Archive errors
Error message: the connection program cannot find the required function library, for example, LD:-LM: no such file or
Directory.
This type of error occurs when the function library connected to the target file is incorrect, probably because the function library name is incorrect,
The directory name of the specified function library is incorrect. The check method is to use the find command to find
Find the corresponding function library name, determine the name of the file library and directory, and modify the name in the program and compilation options.
Category 4: undefined symbols
Error message: Undefined symbol ).
This type of error occurs during the connection process. There may be two reasons: first, the User-Defined letter.
The source code file where the number or global variable is located is not compiled, connected, or not defined yet.
The user needs to modify the source program according to the actual situation and give the definition body of the global variable or function. The second is undefined.
Is a standard library function. This library function is used in the source program.
Specify the name of the corresponding function library or the directory name of the file library.
Run the AR command to check which function library the library function is in. After confirming, modify the GCC
-L and-l items in the connection options.
Excluding errors during compilation and connection, it should be said that this is only the simplest and most basic one in program design.
Step, it can be said that it was just a start. Errors in this process are described in C.
Errors generated by an algorithm are easily excluded. We write a program to compile and connect
So far, it should be said that at the beginning, the problems encountered during the running of the program are problems in algorithm design,
A more mysterious point is that we do not have enough understanding of the problem and need to perform further testing, debugging, and modification. I
Programs, which are a little complex, often need to be compiled, connected, tested, and modified multiple times.
Ii. Analysis and Handling of common error messages
2
1. error message during preprocessing
No such file or directory
Chinese meaning: no corresponding file or directory
Cause of error: the required file cannot be found in the compiler's search path. The file may already exist in the command line.
Or in the # include statement.
Solution: Check whether the file name is correct or whether the directory where the file is stored has been added to the system directory
Or link to the directory. The following is an example:
# Include <stdoi. h>/* error: incorrect file name */
Int main (void ){
Printf ("Hello world! /N ");
Return 0;
}
The above program tries to introduce a non-existent file 'stdoi. H', which will cause the following error:
'Stdoi. h: no such file or directory '. The correct file name should be 'stdio. H '.
Macro or '# include' recursion too deep
Chinese meaning: Macro or '# include' is too deep recursive
Cause of error: the statement # include Nesting is too deep. When the pre-processor encounters too many nested # include
This error occurs. Generally, when two or more files reference each other, this error will cause an infinite number of errors.
Recursion. For example:
/* Foo. H */
# Include "bar. H"
...
/* Bar. H */
# Include "foo. H"
...
The solution is to ensure that each file is not referenced by each other.
Invalid preprocessing directive #...
Illegal preprocessing command #...
Error cause: this error indicates that the pre-processor has encountered an unrecognized # command. For example:
# If foo
Int x = 1;
# Elseif bar/* should be written as # Elif */
Int x = 2;
# Else
Int x = 3;
# Endif
Preprocessor syntax # If block requires the # Elif instead of the "else if" condition, neither
# Elseif.
Warning: this file has des at least one deprecated or
3
Antiquated header.
Warning: The file references at least one obsolete or obsolete header file.
Error cause: this error is often caused by the reference of the old header file library in the C ++ program,
For example, the 'iostream. H' header file does not have the '. H' extension in the modern c ++ library. Import the old header file to its function
To the top-level global namespace, STD: is required for C ++. Of course, for the old style Headers
File is still supported, so the existing program will continue to be compiled, just give a warning. For example:
# Include <iostream. h>/* old style */
Int main (void ){
Cout <"Hello world! /N ";
Return 0;
}
'Iostream. H' should be rewritten to # include <iostream> and STD: cout.
Unterminated '# If 'Conditional
Chinese meaning: '# If' statement condition is not terminated
Error cause: # endif statement missing
2. error message during compilation
'Variable' undeclared (first use in this function)
The variable 'variable' is not declared (this variable is used for the first time)
Solution: declare the variable before use.
Resolution: In C and C ++, variables must be declared and used first. For example:
Int main (void ){
Int I;
J = 0;/* j variable not declared */
Return J;
}
In this example, J is a variable that has not been declared, so an error will be triggered: 'J' undeclared.
Parse error before '...'
Explanation error before 'xxx' statement
Error cause: syntax error
Parsing: Generally, the compiler encounters unexpected input. For example, a non-syntactic string sequence. This error
It may also be caused by the loss of curly braces, garden parentheses, semicolons, or invalid reserved words.
# Include <stdio. h>
Int main (void ){
4
Printf ("hello")/* lost semicolon */
Printf ("world! /N ");
Return 0;
}
Conflicting types for 'Alex'
Meaning: There is a prominent type for the 'Alex 'variable
Cause of error: Other data types may have been declared for 'Alex. For example, two statements exist.
The two 'Alex 'variables are declared as two enumeration (Enum) types.
Two or more data types in Declaration specifiers
Meaning: multiple data types exist in the Declaration identifier
This error is most likely to occur because a ";" is missing in the program. It may be in the header file,
It may also be in this file (the most error is that you forget ";" in the struct).
Too polictypes in Declaration
Meaning: too many types are defined in the Declaration
1. Is the header file contained multiple times? Check for example:
# Ifndef tconfigh
# Define tconfigh
# Endif // whether this is missing
2. Check whether the logical operator relationship is missing between the long statement and the next statement.
Operators and other symbols
Warning: Comparison between pointer and integer
Warning: Compares pointer and integer values
It may be okay. This warning is usually triggered by 'if (strstr (line, "word ")! = NULL) 'and other strstr Functions
Return pointer or null value.
Subscripted value is neither array nor pointer
Chinese meaning: the lower mark value does not meet the array or pointer requirements
Cause of error: an attempt to use a variable as the subscript
Floppyto. C: 782: Parse error at end of input
Chinese meaning: parsing errors at the end of the file
Floppyto. c is the program file name, and 782 is the number of wrong lines, but this number is greater than the file length. Error cause:
No matching curly braces {} or annotation /**/
# Include <stdio. h>
Int main (void ){
If (1 ){
5
Printf ("Hello world! /N ");
Return 0;/* The curly braces do not match */
}
Parse error before 'printf'
Explanation error before 'printf'
Cause of error: the semicolon ';' is missing before the statement ';'
Warning: Implicit declaration of function '...'
Warning: not consistent with the implicit declaration of function xxx
Error cause: this error is generated because the function used has no prototype declaration. Because it can be
Import the wrong header file or forget to provide the function prototype. For example:
Int main (void ){
Printf ("Hello world! /N ");/* No header file */
Return 0;
}
The program does not import the system header file 'stdio. H', so the prototype sound of printf does not exist.
Ming. Correction Method: add a statement # include <stdio. h> at the beginning of the program.
Unterminated string or character constant
Chinese meaning: unterminated string or character constant
Error cause: this error is caused by the absence of pair quotation marks using a string or character constant. Pair of words
Pair single quotation marks should be used, and pair double quotation marks should be used for strings.
# Include <stdio. h>
Int main (void ){
Printf ("Hello world! /N);/* Missing Double quotation marks */
Return 0;
}
Character constant too long
Chinese meaning: The character constant is too long
Cause of error: In C and C ++, character constants are a single character enclosed by single quotes, and
Has the corresponding ASCII value. For example, the ASCII value of 'A' is 67, and the ASCII value of '/N' is 10. This error occurs.
The error is caused by the use of single quotes to encapsulate more than one character. For example:
# Include <stdio. h>
Int main (void ){
Printf ('Hello world! /N');/* incorrect encapsulation */
Return 0;
}
A string consisting of multiple characters is enclosed by double quotation marks. For the above example, it should be rewritten to: "Hello world! ".
6
Warning: initialization makes integer from pointer without
Cast
Warning: cannot be converted from pointer to integer during initialization
Error cause: this error indicates pointer misuse in the integer environment. Technically, the pointer and
Integer numbers are converted to each other, but these are rarely used in external system-level applications. Generally, such warnings are
If a pointer is used, the pointer is not released. (For example, if the statement is written as int I = P rather than int I =
* P, where p is the pointer ). This warning may also be triggered during conversion between char and char * types, because
Char is also an integer.
Int main (void ){
Char c = "/N";/* incorrect */
Return 0;
}
The variable C is of the char type, and "/N" is a string and is considered as a const char * pointer by the system.
(It will occupy 2-byte memory, including '/0' because the string has no Terminator ). Similar errors occur in
Misuse of macro NULL:
# Include <stdlib. h>
Int main (void ){
Int I = NULL;/* incorrect */
Return 0;
}
In 'stdlib. H' of C, macro null is defined as (void *) 0. It can only be used in the pointer environment.
Dereferencing pointer to incomplete type
Meaning: indirect reference pointer is an inappropriate type
Cause of error: The program attempts to access an element in the body of a structure that is not declared in advance through a pointer. In C
In C ++, when declaring a pointer to a structure, the structure should be declared first.
Struct btree * data;
Int main (void ){
Data-> size = 0;/* unsuitable type */
Return 0;
}
This program uses the forward declaration of the btree structure data. However, before the pointer indirectly accesses the memory independently,
This structure needs to be defined.
Warning: Unknown escape sequence '...'
Warning: Unknown escape sequence
Error cause: Incorrect escape characters are used. The valid Escape Character Sequence is as follows:
/N New Line/T Tab
/B rollback/R carriage return
/F form feed/V vertical tabulation
7
/A warning (ringtone)
Combination character //,/',/"and /? It indicates the corresponding independent characters. The escape sequence can also use an octal code.
/0 --/377 and hexadecimal code/0x00 --/0xff.
# Include <stdio. h>
Int main (void ){
Printf ("Hello world! /N ");/*/n Error */
Return 0;
}
Warning: Suggest parentheses around assignment used as truth
Value
Warning: We recommend that you use parentheses to enclose the value assignment expression used for logical values.
Cause of error: the warning emphasizes potential semantic errors. The program is tested in conditional statements or other logical expressions.
In the test, the value assignment operator '=' is used instead of the comparison operator '= '. Of course, in syntax, the value assignment operator
It can be used as a logical value, but is rarely used in practice.
# Include <stdio. h>
Int main (void ){
Int I = 0;
If (I = 1) {/* = should be = */
Printf ("unexpected result/N ");
}
Return 0;
}
Warning: control reaches end of non-void Function
Warning: control to the end of a non-void function.
Cause of error: If a function has been declared as a returned data type (such as Int or double ),
Return a value of the corresponding type using the return statement at an appropriate position in the function (all possible ending points.
Otherwise, it is not a well-defined function. If the function is declared as void, the return statement is not required.
# Include <stdio. h>
Int display (const char * Str ){
Printf ("% s/n", STR );
}
The above program does not return a statement at the end of the display function, but it is declared to return an int class.
Type. You can solve this problem by adding statements such as return 0. When GCC is used
The main function of must return an int type data (used to indicate the exit status of the program ).
Warning: unused variable '...'
Warning: There is an unused variable XXX.
Warning: Unused parameter '...'
Warning: There is an Unused parameter XXX.
8
Cause of error: This warning indicates that a variable or function parameter has been declared as a local parameter, but elsewhere
I have never used it. Variables that have not been used may cause procedural errors, such as occasional write at the expected position
Different variable names.
Int Foo (int K, char * P ){
Int I, J;
J = K;
Return J;
}
In this program, variable I and parameter P have never been used.
Warning: Passing Arg of... as... due to prototype
Warning: the input parameter XX is XXX, but the prototype does not match.
Cause of error: This warning indicates that there is a inconsistency with the declared parameter Type prototype when calling the function.
Status.
Warning: Passing Arg 1 of 'cpystr' makes integer from pointer
Warning: type conversion is not allowed if parameter 1 of a function does not match.
Error cause: type conversion Mismatch
The following example shows the problem:
Void cpystr (char item );
Main (){
Char SRC [] = "Martin Leslie ";
Cpystr (SRC );
}
Cpystr (char item ){}
The function should be written in a similar format as follows:
Void cpystr (char item []);
Main (){
Char SRC [] = "Martin Leslie ";
Cpystr (SRC );
}
Cpystr (char item []) {}
Warning: Assignment of read-only location
Warning: assign values to read-only variables.
Cause of error: Check whether the assigned variable has been modified with const or has been declared as a constant.
Warning: Cast discards qualifiers from pointer target type
Warning: an inappropriate conversion qualifier exists in the pointer target type.
9
Warning: Assignment discards qualifiers...
English meaning: Warning: assign a value to discard the qualifier XXX.
Warning: initialization discards qualifiers...
English meaning: warning: the initialization discard qualifier XXX.
Warning: Return discards qualifiers...
Chinese meaning: warning: the return value is discarded with the qualifier XXX.
Cause of error: Invalid Pointer usage. Check whether the assigned variable has been used.
Const modifier or has been declared as a constant. If the pointer is limited to const, the pointer cannot be modified, and
It can only be used to assign values to other pointers.
Char * F (const char * s ){
* S = '/0';/* value to read-only data */
Return s;/* will discard the constant, that is, it cannot be returned */
}
This program attempts to modify constant data and uses constant attribute parameters in the return value, leading to discarding.
Initializer element is not a constant
The initialization element is not a constant.
Cause of error: in C, a global variable can only be a value constant during initialization, such as a value, null, or word.
String constant. This error is thrown if a constant value is used.
# Include <stdio. h>
File * stream = stdout;/* not a constant */
Int I = 10;
Int J = 2 * I;/* not a constant */
Int main (void ){
Fprintf (stream, "Hello world! /N ");
Return 0;
}
Note: In C ++, a very large amount of data can be used during initialization.
3. Link error messages
File not recognized: file format not recognized
Chinese meaning: the file format is not recognizable.
Cause of error: the file extension is not '. C '.
# Include <stdio. h>
Int main (void ){
Printf ("Hello world! /N ");
Return 0;
}
If the above file is saved as 'hello' but there is no extension, the following error is returned during compilation:
10
$ Gcc-wall hello
Hello: file not recognized: file format not
Recognized
Collect2: LD returned 1 exit status
The solution is to rename the file with the appropriate extension. This example can be renamed as 'hello. C '.
Undefined reference to 'foo'
Collect2: LD returned 1 exit status
Chinese meaning: no reference to 'foo' is defined.
Cause of error: The program uses functions or variables that are not defined in this file and other libraries. Yes
The Link Library is lost or incorrect name is used.
In this example, 'collect2' is part of the linked program.
Int Foo (void );
Int main (void ){
Foo ();
Return 0;
}
/Usr/lib/crt1.o (. Text + 0x18): Undefined reference to 'main'
Definition: Reference to the main function is not defined.
Cause of error: the main () function is missing in the program.
/Usr/lib/crt0.o: Undefined symbol _ main referenced from text
Segment
Chinese meaning: Reference undefined _ main characters from text segments
Error cause: the main () function is missing-but in fact, why?
1. A syntax error may exist in the header file;
2. The GCC command may be missing in the C source file.
Undefined symbol _ initscr referenced from Text Segment
Chinese meaning: Reference undefined _ initscr characters from text segments
Cause of error: a function is called but does not exist, or the # include statement does not contain
The library of the function.
4. error message during running
Error while loading shared libraries:
Cannot open shared object file: no such file or directory
An error occurred while loading the Shared Library:
Shared object file cannot be opened: the file or directory does not exist
11
Cause of error: Shared libraries are used in the program, but the required shared libraries cannot be found through dynamic links at program startup.
Shared library files. If yes, modify the value of the shared library search PATH variable LD_LIBRARY_PATH.
Segmentation fault
Chinese meaning: Segment error, Bus Error
Cause of error: attempt to access protected content or overwrite important data! It indicates a memory access error.
The common reasons are as follows:
1. a null pointer or uninitialized pointer is referenced in the reverse direction;
2. The subscript accessed by the array is exceeded;
3. incorrect use of malloc, free, and related functions;
4. The parameter (quantity and type) When scanf is used is incorrect.
Floating Point exception
Chinese meaning: Floating Point Operation exception
Error cause: This is an arithmetic operation exception. If the divisor is 0, overflow, overflow, or illegal operations (such
Returns the square root of-1 ).
Illegal instruction
Chinese meaning: Invalid Command
Cause of error: this error occurs when the system encounters an invalid machine command. Generally, this type of error occurs in the source
After the code has been compiled into the target code of a specific machine, it will run on another type of machine.

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.