Common gcc Errors and Their Meanings
Debugging is a major part of any software
development. "In a typical commercial development
organization, the cost of providing this assurance
via appropriate debugging, testing, and verification
activities can easily range from 50 to 75 percent of
the total development cost."1
While this is not a course about the software
development life cycle it is our goal to teach you
good software development technique.
The errors that you typically experience are one of
three types:
- Compiler Errors
- Linker Errors
- Run Time Errors
lets examine these three types of errors in more detail.
Compiler Errors
Compiler errors are caused by incorrect syntax. This
means that you have not followed the basic rules of C. These basic
rules are things like: - putting a semicolon at the end of a
line
- writing a proper function header
- passing the correct number and type of arguments to a function
- matching up all pairs of braces {} and parentheses ()
- not using variables that you haven't declared
- declaring all variables at the top of a function
- many, many more
When you run the compiler on your code it first does any preprocessing
(that is any line that begins with #). Then it checks the syntax of
your code. In other words it makes sure that you have followed all the
rules of C. If you haven't it prints out an error so that you can fix
your syntax. Unfortunately most new users find the messages sent back
from gcc confusing. So we will go over some common gcc error messages,
what they mean, and how to fix them.
General compile-time error and warning description
In C, a compilation error is usually fatal, meaning that the C compiler
cannot compile the source code. A warning, on the other had, is usually
just that. The compiler has identified that there may be a problem, but it
can produce object code anyway. Warnings should not be ignored, because
they usually do indicate that there is something wrong with the program,
and it is likely to behave differently from what you would expect.
Error messages and warnings are preceded by the program file name and function
in which the error was encountered. For example,
prog.c: In function `main':
indicates that the error was encountered in the program file prog.c,
and specifically in function main. The next lines indicate the
errors/warnings that occur in that function and program file. If the
program is multi-function and/or multi-file, then each different
function/file containing errors will be listed separately.
The warning/error lines following the function/program file identification
line is a record of the program file containing the error; the line number
in the source file at which the error occurred; the string "warning", if the
message is just a warning (nothing is printed if it is an error); and a
description of the problem encountered. For example,
prog.c:3: warning: unknown escape sequence `\z'
indicates that a problem was encountered at line 3 of the program file
prog.c. The message is just a warning. The problem encountered
is that the character `\z' is not a known escape sequence. If there are no
other errors, you will be able to run the program, but it will
probably produce results which are different from what you wanted.
'variable' undeclared (first use in this function)
C is a typed language, which means that you must declare variables before
you can use them. C is also case-sensitive, so that var and
Var are considered to be different variables. You have either
forgotten to declare a variable before using it, or else you have mistyped
a variable name. This error message is usually accompanied by (Each undeclared identifier is reported only once for each function it appears in.).
parse error before 'XXXXX'
The C compiler has encountered something that it doesn't recognize, and it
cannot figure out what it might be. C statements usually start with a
reserved word, a variable name, or a function name. This error is normally
generated when the first word of a statement is unrecognized, and it cannot
possibly be a function call, variable name,
etc. e.g.,
main() {
silly printf("Hello, world\n");
}
Alternatively, something, perhaps a close bracket ()) or a close
brace (}), is missing at or around the indicated line, before the
character or string indicated by XXXXX. You should check to
make sure that you don't have too many brackets! You might also have a
semi-colon (;) missing at the end of the previous statement.
too few arguments to function `YYYYY'
This error is exactly what it sounds like. You have called a function
and you did not send it enough arguments. To correct this you should
check how many arguments the function needs and send that many.
type mismatch with previous implicit declaration
and
previous implicit declaration of `ZZZZZ'
This error message means that you have failed to write a function
prototype for the function ZZZZZ before you used that function. Go up
to the top of the file and add a prototype.
parse error at end of input
You probably have a missing close brace (}) somewhere in your
program. The compiler cannot tell where it is missing from, so happy hunting!
unterminated string or character constant
You have an unbalanced number of single or double quotation
marks. Check your program for a place where you started a string but
forgot to put the closing quotation mark.
character constant too long
In C, strings must be surrounded by double quotation marks (""). If you
have used single quotation marks (''), then C expects to find a single
character (or an escape character). It is likely that you have put
'/n' instead of '\n'. You may have used single quotes instead of double
quotes for instance
printf('x = %d', num); instead of printf("x = %d", num);
warning: passing arg n of `XXXXX' makes
pointer from integer without a cast
This is one warning that should NEVER be ignored. You have
called the function XXXXX. The argument n
is expected to be a pointer (we will learn about pointers later
on), but you have passed an integer instead. A type cast explicitly
changes the type of data from one type to another, but you haven't
used one. Note that because this is only a warning the compiler will
generate an a.out file. However, unless you know exactly what you are doing
this will almost certainly cause your program to crash even though it
is only a warning. This often happens when you send a single char to a
function that is expecting a char array. Check to make sure that you
did not forget to send a format string to printf or scanf.
Linker errors
There may be a lot of different types of compiler errors, but they are
generally not considered to be a big problem by professional
programmers. A compiler error always tells you what line it occurred on
(or close) and what the problem was. This is not the case for linker
or compiler errors. There are two pieces of good news though. First if
you are getting a linker error then all of your syntax is correct
because the compiler has moved past the syntax checking stage and onto
the linking stage. The other bit of good news is that you will
probably only run into one type of linker error in this course. It
looks like this:
Undefined first referenced
symbol in file
main /usr/local/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
This error message should be read top to bottom not right to left. It
says "Undefined symbol main first referenced in file /usr/..." This
message is generated because gcc has encountered what looks like a
function call, but there is no function with that name. The name of
the "missing" function is the first word of line 3 of the error
message (main). If you get one of these errors check to make
sure that you have written the function and that you have spelled the
function name correctly.
Run Time Errors
There are many different types of run time errors. They are much more
difficult to get rid of than compiler and linker errors. There are
even special pieces of software called debuggers that have been
written to help people hunt for run time errors. Run time errors can
be logic errors that cause your program to produce bad output or they
can be fatal errors that crash your program when you run it. Two
important fatal run time errors are discussed below:
Floating exception (core dumped)
This is caused by a division by zero in your program. Check each
division in your program and make sure that the denominator can never
become zero.
Segmentation fault (core dumped)
Also called a segmentation violation or simply SEGV. This error will
cause you no end of trouble. It happens when you access memory that
does not belong to your program. We will learn a lot more about this
when we discuss pointers and dynamic memory. For right now if you get
a segmentation fault you should check several things. (1) make sure
that you have a format string as the first argument of all printf and
scanf calls. (2) Check all of your scanf calls. Any variable in a
scanf call that is not an array should have and ampersand (&) in
front of it. You will understand why later. (3) Make sure that you are
not accessing any array elements that are past the end of the
array. For instance if you have char name[20]; make sure that
you do not do something like name[25] = 'n'; When we learn
about pointers you will understand segmentation faults much better.
1 B. Hailpern and P. Santhanam, "Software debugging,
testing, and verification" IBM Systems Journal Volume 41, Number 1, 2002