Frequently Encountered Compiler Messages

Introduction

This page lists some compiler errors you may encounter, and discusses what you should look for in your code for each error.  The exact wording of the compiler message will vary from compiler to compiler, so you must use some common sense when reading the message.  All examples below are taken from the Franklin compiler.

Tips on reading compiler error messages
Compiler messages

Tips on reading compiler messages

1) Often, the first couple of lines of an error message will simply be information on where an error happened.  You can safely ignore this information most of the time.   For example, in the following error message:

test.cpp: In function `int main(...)':
test.cpp:8: no matching function for call to `basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >::junk ()'

The first line is simply telling us that the next message refers to something that happened in the main () function.  Since there's a line number on the next message anyway, we would have been able to figure out where the error happened. 

2) Always, always, always fix the first two or three compiler messages and then recompile.  Anything past the first two or three messages might be caused by earlier messages, and may go away once you fix the first ones.  If you are getting so many compiler messages you can't see the first ones, use the "script" command to put the compiler messages into a file you can look at in vi.  For example (what you type in is in bold.  Normal text is what Unix displays to you):

>script
Script started, file is typescript
>g++ test.cpp
...some error messages appear here...
>exit
Script done, file is typescript
>vi typescript

3) If you see something in the message like "basic_string <char, xxx>" where "xxx" is some long text, you can safely substitute "string" for that part of the message.  For example, the following code:

string temp;

temp.junk ();

Gives an error message:

test.cpp: In function `int main(...)':
test.cpp:8: no matching function for call to `basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >::junk ()'

You should read it like this:

test.cpp: In function `int main(...)':
test.cpp:8: no matching function for call to `string::junk ()'

4) If you are using templates, and see an error message involving the template class, you can safely ignore any template parameters that you didn't use in your code.  For example, code like this:

vector<int> aVector;

aVector.junk ();

Gives this error message:

test.cpp: In function `int main(...)':
test.cpp:8: no matching function for call to `vector<int,allocator<int> >::junk ()'

Since you only provided one template parameter to the vector, ignore the second template parameter in the error message and read the error as:

test.cpp: In function `int main(...)':
test.cpp:8: no matching function for call to `vector<int>::junk ()'

Compiler Messages

implicit declaration of function `int foo(...)'
Symbol is multiply defined
Undefined symbol

This list will grow as I encounter other compiler messages that seem common enough to include here.  If you find a compiler message you think should be here, send me email with the exact text of the message and the source file that cause the compiler message (edit the source file to the minumum code necessary to generate the compiler message). 

implicit declaration of function `int foo(...)'

Example:

test.cpp: In function `int main(...)':
test.cpp:5: implicit declaration of function `int foo(...)'

This error message means that we have attempted to call a function without having a prototype for the function.  If C++ encounters a function call before it encounters the function prototype for the function, C++ assumes that the function returns an int.

If you think you have a prototype for the function, look again.  You have probably changed the parameters the function takes and not updated the prototype.  In any case, the function prototype you do have does not match what you are trying to pass into the function.

Undefined symbol

Example:

Undefined                  first referenced
   symbol                      in file
foo(int)                   /var/tmp/ccOTZyNA.o
ld: fatal: Symbol referencing errors. No output written to a.out

An undefined symbol error is a linking error (the "ld:" means that the error happened during linking).  This means that the program compiled okay, but during linking it could not find something it was looking for.  In this case is is the function "foo (int)" that could not be found.

This could be caused several ways.  One possibility is that the function definition does not match the function prototype (check the number and type of arguments, and also check the exact spelling of the function name).   Another possibility is that you are using multiple source files and have not compiled all of the source files together.

Another common way to get this error is to use the /* */ style comments, but forget to put in the closing */.  Everything in the rest of your source file becomes a comment and will not be compiled.  For this reason I generally use the // style comments.

If you are writing a class, make sure you have "class::" in front of the function definition in the .cpp file.

Symbol is multiply defined

Example:

ld: fatal: symbol `foo(int)' is multiply defined:

This error means that you have a function definition appearing more than once in your code.  Typically the function appears in multiple source files, so each source file compiles okay individually, but when you try to compile them together, you get this error.  

If the symbol that is multiply defined is "main (...)", then you have a main function in more than one source file.  You should only have one main function in a single program, regardless of how many source files you use.