GCC Common errors

Source: Internet
Author: User
Tags integer numbers parse error

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 fileOrDirectory
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 fileOrDirectory '. The correct file name should be 'stdio. H '.
MacroOr'# 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 deprecatedOr
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 useInThis 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;
}
ConflictingTypesFor '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 inDeclaration 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 functionsTypes 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: implicitDeclarationOf 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 stringOrCharacter 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 btree structureDataForward declaration. 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 fileOrDirectory
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.

GCC Common errors

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.