Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error LNK2001: unresolved external symbol (C++) [duplicate]

Tags:

c++

visual-c++

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?

like image 242
user2549990 Avatar asked Jul 09 '13 06:07

user2549990


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.

like image 126
AnT Avatar answered Nov 02 '22 14:11

AnT


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.

like image 34
Payton Wu Avatar answered Nov 02 '22 15:11

Payton Wu