What is an undefined reference/unresolved external symbol error and how do I fix it?

It depends on where you see it, during compile or link. It means you've used something without declaring or defining it, which one depends on what you've done.
Last edited on
Usually the error also says where the bad reference comes from.

Read the symbol name carefully. I think the most likely cause of this error is that you've misspelled something. I do this all the time:
count << xyz;
because my code more likely to have a variable named count and my fingers just type that instead of cout automatically.

Another likely cause is that you've failed to include a library or object code file in your build. If you think the symbol should be defined somewhere then make sure you're including that file.

I'm not sure about modern compilers, but in the ancient ones we use at work, libraries are linked in the order they appear on the command line and the only link in the symbols that are undefined at that point. So if you have this:
lib1 defines symbol x
lib2 uses symbol x

Then you have to link lib2 before lib1. If you link lib1 first, then the linker has no reference to symbol x at the time it's linking lib1, so it doesn't include that symbol. Only when it links lib2 does it find the reference and it doesn't go back to get the previously skipped symbol.
Registered users can post here. Sign in or register to post.