It follows a description of the messages that are most commonly shown by the compiler. Sample code is shown to illustrate the cause of the error, but each program has a different structure and therefore, the error may appear due to some different high level causes.
`variable' undeclared (first use in this
function)
This is one of the most common and easier to detect. The symbol shown at the beginning of the message is used but has not been declared. Only the first appearance is shown.
warning: implicit declaration of function
`...'
This warning appears when the compiler finds a function used in the code but no previous information has been given about it. The compiler assumes that the function returns an integer and proceeds.
These warnings are those that we recommend you eliminate before trying the execution of the program. Depending on the situation, there are several ways to eliminate this message. The compiler needs to know how the found function is defined. For that, in a previous location in the code the file must included, either the entire function definition or its prototype (the data type of the result followed by the name, a parenthesis with the parameter definition and a semicolon.
If this information (the function definition) is needed
in several files, then, instead of writing the same information in all
of them, it is recommended to include the function prototype in a file
which is then included where needed through the use of the
#include
pre-processor directive. Remember that the code of
a function may only appear in a single location in the code. If the
compiler finds the function body in more than one location an error will
be shown to flag the multiply defined function.
It may be also the case that the required information is contained in a file to be included because the function is part of a library. To check if a function is in any of the C standard libraries we recommend the use of the man command followed by the function name. If the function is part of a library, the manual page shows the file that needs to be included in our code (if any).
warning: initialization makes integer from pointer without a
cast
This warning tells us that we have used a pointer in a context where an integer is expected. This is allowed in C and the compiler knows how to perform the translation, but it warns us just in case we want to “confirm” the operation using a cast. The following code shows an example that produces this warning.
int main (void)
{
char c = "\n"; /* incorrect? */
return 0;
}
In the first line a pointer to a character (because an string is
internally stored as a memory address, which indicates the address where the first
letter of the string is located) is assigned to a character.
Although c
is defined as a character, the
compiler treats it as an integer, and this is legal. The string
"\n"
is taken as a pointer and that is the reason for the
warning message. A pointer is being assigned to a variable that is not a
pointer, it may be considered as an integer and furthermore, a casting
is not being used.
In most of the cases, this assignment is in fact a programmer error and needs to be fixed. But it is possible that a pointer has to be manipulated as a integer as it is shown in the following program:
int main (void)
{
int value = "\n"; /* Correct Assignment */
return 0;
}
Let us assume that in this case the programmer does need to store this pointer in an integer variable. The compiler still shows the warning. To confirm the compiler that this is really our intent, a casting can be used, that is, a prefix to the symbol name where the new type is written in parenthesis. Once added this prefix, the compiler understands that this assignment is truly what the programmer wants, and the warning is not shown.
int main (void)
{
int value = (int)"\n"; /* Correct assignment confirmed by the programmer
using the cast */
return 0;
}
dereferencing pointer to incomplete type
This error appears when a pointer to a structure is used to access to one of its fields but the compiler does not have enough information about the structure. The following program shows this situation:
struct btree * data;
int main (void)
{
data->size = 0; /* Incomplete information */
return 0;
}
The declaration of variable data
as a
pointer to a btree
structure is correct. Declaring a
pointer to a structure does not require the structure to be defined. But
in the first line of function main
this pointer is used to
access one of the fields in the structure. In this case, the definition
of the structure is not present and the error is shown.
The most probable cause for this error is that the
definition of the structure is in another file and must be present
before using the pointer to access a field. For this reason, the
structure definitions are typically written in files with extension
that are then included in the code
files.*.h
warning: control reaches end of non-void
function
This warning appears when a function has been defined as
returning a result but no return
statement has been
included to return this result. Thus, either the function is incorrectly
defined or the statement is missing. The compiler is still capable of
generating, and even though the function does not contain the statement,
a result is returned to the function that performed the
invocation.
warning: unused variable `...'
This warning is printed by the compiler when a variable is declared but not used in the code. The message disappears if the declaration is removed.
undefined reference to `...'
This message appears when there is a function invoked in the code that has not been defined anywhere. The compiler is telling us that there is a reference to a function with no definition. Check which function is missing and make sure its definition is compiled.
error: conflicting types for `...'
Two definitions of a function prototype have been found. One is the prototype (the result type, name, parenthesis including the parameters, and a semicolon) and the other is the definition with the function body. The information in both places is not identical, and a conflict has been detected. The compiler shows you in which line the conflict appears and the previous definition that caused the contradiction.