Say I have this function called DoThis(const char *abc) in a file called one.cpp. So when I attempt to call this function from another function in a different source file (two.cpp), I get the error: error LNK2001: unresolved external symbol (C++), even though I used #include "one.h" What would I do to fix this?
-
Is it also defined in one.h – Enigma Jul 09 '13 at 06:22
-
The function DoThis is defined in the header. – user2549990 Jul 09 '13 at 06:24
-
are both files linked to the project? – Enigma Jul 09 '13 at 06:24
-
Yes they are linked together. – user2549990 Jul 09 '13 at 06:27
-
How do you compile the programs? – 0decimal0 Jul 09 '13 at 06:27
-
Are you sure that the signature is exactly the same? Not typos in the name? – Korchkidu Jul 09 '13 at 06:27
-
Don't worry, I solved it. Just add cdecl to the function ref in the header – user2549990 Jul 09 '13 at 06:34
-
@user2549990: You first stated that the function is defined in a `.cpp` file. Now you are saying that it is defined in the header (???). Do you actually understand what "defined" means? Function definition is the *function body*. Where does the function body for `DoThis` reside? – AnT stands with Russia Jul 09 '13 at 06:41
-
1This is a linking related error having nothing to do with headers. – SChepurin Jul 09 '13 at 06:46
-
I had an error where my project was compiled as **x64** project. and I've used a **Library** that was compiled as **x86**. I've recompiled the library as x64 and it solved it. – Gal Bracha Feb 06 '17 at 13:56
2 Answers
That means that the definition of your function is not present in your program. You forgot to add that one.cpp
to your program.
What "to add" means in this case depends on your build environment and its terminology. In MSVC (since you are apparently use MSVC) you'd have to add one.cpp
to the project.
In more practical terms, applicable to all typical build methodologies, when you link you program, the object file created form one.cpp
is missing.

- 312,472
- 42
- 525
- 765
-
But what if say my file is a header only file cause its an abstract class? Sorry to dig up old post :) – Niklas Vest Apr 01 '16 at 15:27
-
@Niklas Vest: If everything is there, in the header, then the compiler/linker should be able to find everything without any problems. I.e. LNK2001 simply will not occur. Placing inappropriate things into header files usually elevates risk of LNK2005, which is on the other end of the spectrum: too many definitions. – AnT stands with Russia Apr 01 '16 at 16:52
Sounds like you are using Microsoft Visual C++. If that is the case, then the most possibility is that you don't compile your two.cpp with one.cpp (one.cpp is the implementation for one.h).
If you are from command line (cmd.exe), then try this first: cl -o two.exe one.cpp two.cpp
If you are from IDE, right click on the project name from Solution Explore. Then choose Add, Existing Item.... Add one.cpp into your project.

- 69
- 2