Link error: "unresolved external symbol"

Question | Answer


Back to questions and answers table of contents.

This is an archive of questions and answers relating to the PC version of the CS Libraries. If you don't see what you're looking for here, contact cs106-pchelp@cs.stanford.edu.


Question:

I am getting the link error "unresolved external symbol" for functions that were provided with the assignment starter project.  I checked and the function name is the same as listed in the .h file.

Example: I am working on the battleship assignment,and I have a problem with the battlegraphics.h library.

I get the following message from my compiler (MS Visual C++ 6.0):

Linking...
BATTLESH.OBJ : error LNK2001: unresolved external symbol _DrawStartingBoard
Debug/battleship.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe. 
battleship.exe - 2 error(s), 3 warning(s)

What's going on?

Answer:

There's a difference between interface and implementation.

A .h file (such as battlegraphics.h) defines the interface for a group of related functions.  That is, it tells you (and the compiler) what functions exist, how to call them, what their arguments and return types are, etc.  But you'll notice that there is no code there that would tell the compiler what steps to carry out if you call DrawStartingBoard() -- only that such a function exists and takes such and such arguments.

That is where the implementation comes in.  The implementation of a function is the code that defines its execution.  This can be in a .c file; for example, in the course of a normal assignment or program, you implement many functions.   In some cases, we will provide you with .c files containing the implementation for functions that may use as-is, look at, or modify.  The implementation can also be contained in a .lib file (library) containing already-compiled code; we may provide you with .lib files instead of .c files in cases where the implementation source is unimportant or needs to remain a secret.

Either way, when you #include a .h file, you've only told the compiler what functions and variables you later intend to define or implement.  You also have to supply these implementations; this is done by adding files (.c or .lib) containing the implementations to your project.

The compiler compiles your source files one by one, leaving "external" references to things that were mentioned in .h files but never actually defined.   Then the linker "links" or combines the results of the compilation and any libraries in the project, patching up the "external" references.   That's why you got a linker error, not a compiler error.

For the above example, adding battlegraphics.lib to the project should fix the problem. It doesn't matter where the .lib file is on disk, so if you copied it to the project folder, that won't get it added. Go to the menubar and choose Project | Add to Project | Files... and then browse to the directory where you placed the .lib files, select it and click OK.


PC CSLib info / Erik Neuenschwander / erikn@cs.stanford.edu (questions about page)
cs106-pchelp@cs.stanford.edu (questions about CS Libraries)