C++ error LNK2001: unresolved external symbol

A quick Visual C++ tip to help those who get stuck on this problem like I did. If you find yourself getting errors like this:

foo.obj : error LNK2001: unresolved external symbol "public: __thiscall MyMatrix::~MyMatrix(void)" (??1MyMatrix@@QAE@XZ)
foo.obj : error LNK2001: unresolved external symbol "public: __thiscall MyMatrix::MyMatrix(class MyMatrix const &)" (??0MyMatrix@@QAE@ABV0@@Z)
fooData.obj : error LNK2001: unresolved external symbol "public: __thiscall MyMatrix::MyMatrix(unsigned int,unsigned int)" (??0MyMatrix@@QAE@II@Z)
bar.obj : error LNK2001: unresolved external symbol "public: class MyMatrix & __thiscall MyMatrix::resize(unsigned int,unsigned int)" (?resize@MyMatrix@@QAEAAV1@II@Z)
.Release2MCProject-32bit-noxlw.exe : fatal error LNK1120: 4 unresolved externals

it could mean that certain methods are declared but not defined. In other words there may be methods in the header files that have no implementations in cpp files. Above we can see that a destructor, two constructors and a resize call are what the compiler’s complaining about. This was because those methods had not been implemented but were in the header file.

Though the really odd thing is that this project was compiling in 32 bit mode and when I tried to port it to 64 bit mode suddenly these errors crept up. I don’t know why these errors didn’t occur in 32 bit mode. Must be yet another compiling intricacy that I’m not aware of.

C++ can be a nightmare compared to higher level languages.

2 thoughts on “C++ error LNK2001: unresolved external symbol

  1. Bruce

    Something that I keep forgetting is the need to define fields too – e.g. adding ‘int MyMatrix::x’ to the .cpp file if ‘int x’ is in the header file.

  2. Dhruba Bandopadhyay Post author

    Ah. I haven’t made that mistake yet as the header files I work with only have methods, constructors and destructors.

    But in general the more I work with C++ the more I realise just how counterproductive it is compared to Java and just how many pitfalls there are that one could fall into on a regular basis. Suddenly the reasons behind the design decisions of Java seem so clear to me in retrospect. Nothing like native languages to make you appreciate Java once more! C# is nice too but not when it’s wrapping C calls underneath as is the case with the Cloo OpenCL bindings!

    A comparison of how times have moved on between native and managed languages would have made a good post but sadly I haven’t been making notes over time so will probably forget many points if I tried to do one off top of head.


Leave a Reply