Unresolved external symbol in object files
Solution 1
This error often means that some function has a declaration, but not a definition.
Example:
// A.hpp
class A
{
public:
void myFunc(); // Function declaration
};
// A.cpp
// Function definition
void A::myFunc()
{
// do stuff
}
In your case, the definition cannot be found. The issue could be that you are including a header file, which brings in some function declarations, but you either:
- do not define the functions in your cpp file (if you wrote this code yourself)
- do not include the lib/dll file that contains the definitions
A common mistake is that you define a function as a standalone and forget the class selector, e.g. A::
, in your .cpp file:
Wrong: void myFunc() { /* do stuff */ }
Right: void A::myFunc() { /* do stuff */ }
Solution 2
Check you are including all the source files within your solution that you are referencing.
If you are not including the source file (and thus the implementation) for the class Field
in your project it won't be built and you will be unable to link during compilation.
Alternatively, perhaps you are using a static or dynamic library and have forgotten to tell the linker about the .lib
s?
Solution 3
It looks to be missing a library or include, you can try to figure out what class of your library that have getName, getType etc ... and put that in the header file or using #include
.
Also if these happen to be from an external library, make sure you reference to them on your project file. For example, if this class belongs to an abc.lib then in your Visual Studio
- Click on Project Properties.
- Go to Configuration Properties, C/C++, Generate, verify you point to the abc.lib location under Additional Include Directories. Under Linker, Input, make sure you have the abc.lib under Additional Dependencies.
Solution 4
I've just seen the problem I can't call a function from main in .cpp file, correctly declared in .h file and defined in .c file. Encountered a linker error. Meanwhile I can call function from usual .c file. Possibly it depends on call convention. Solution was to add following preproc lines in every .h file:
#ifdef __cplusplus
extern "C"
{
#endif
and these in the end
#ifdef __cplusplus
}
#endif
Solution 5
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.
Related videos on Youtube
Novellizator
I study at Charles university. Some of my interests are: Node.js, javascript programming, software design, machine learning and dancing ;) My homepage: http://novellizator.cz
Updated on July 17, 2022Comments
-
Novellizator almost 2 years
During coding in Visual Studio I got an unresolved external symbol error and I've got no idea what to do. I don't know what's wrong. Could you please decipher me? Where should I be looking for what kind of errors?
1>Form.obj : error LNK2019: unresolved external symbol "public: class Field * __thiscall Field::addField(class Field *)" (?addField@Field@@QAEPAV1@PAV1@@Z) referenced in function "public: void __thiscall Form::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Form@@QAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 1>Form.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Field::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Field@@UAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall InputField::InputField(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (??0InputField@@QAE@AAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 1>Form.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Field::prompt(void)" (?prompt@Field@@UAEXXZ) 1>Form.obj : error LNK2001: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Field::getName(void)" (?getName@Field@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) 1>Form.obj : error LNK2001: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Field::getType(void)" (?getType@Field@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) 1>Form.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Field::describe(void)" (?describe@Field@@UAEXXZ) 1>C:\Users\tomy\Documents\Visual Studio 2010\Projects\zapoctovkac++\Debug\zapoctovkac++.exe : fatal error LNK1120: 6 unresolved externals
-
deong about 12 yearsAn unresolved symbol is one that you've declared somewhere but never defined. Usually, it means you've #included some third party library's header file but not told the linker where to find the corresponding .obj files for the library.
-
jave.web over 9 yearsPretty common mistake is that you define a function as a standalone and forget the class selector in your .cpp file: You do this (wrong):
void myFunc() { /* do stuff */ }
Instead of this (right):void A::myFunc() { /* do stuff */ }
-
Patapoom over 6 yearsYou can also add brackets directly in your header file if you don't want to define it more in your .cpp file, like that:
void myFunc() {};
. -
Matt Arnold almost 4 years@deong How do you tell the linker where to find the corresponding .obj files and where would they usually be?
-
-
tmj almost 10 yearsHow to include the said lib file in my project?
-
Chris Morris almost 10 years@tMJ It depends on what environment you are using. I'd look up tutorials online, or on this site.
-
tmj almost 10 years@ChrisMorris The function's definition wasn't available not because I did not link it properly or something. But, because the dll was not in memory and had to be loaded via a LoadLibrary call. (FTR)
-
Charles about 9 yearsThe last advice was exactly the problem here. I was doing
void myFunc() {}
instead ofA::void myFunc() {}
. -
Allen Linatoc almost 9 yearsHow about using
#pragma once
? -
moffeltje about 8 yearsAn example would be highly useful here.
-
RoG almost 7 yearsBrilliant answer. I had actually forgotten both (1) and then the A:: part after copying the method in from elsewhere.
-
zak over 6 yearsRefering the correct lib files resolved the issue. Use Project->Properties->Linker->General->Additional Library Directories and Project->Properties->Linker->Input->Additional Dependencies to refer the lib directory and the lib files
-
Johann Studanski about 6 yearsTo whomever downvoted this: Leave at least a comment why you think the answer is wrong or not helpful. A downvote without a comment is worthless at best.
-
sss about 5 yearsthanks, this causes the issue from my setup ('No (/Zc:wchar_t-)')
-
Top-Master over 3 yearsIn my case, a simple
\
in the Qt project's *.pro file was missing (should have beenSOURCES += main.cpp \
to continue to next line). -
PJ127 over 3 yearsThis answers refers to a good solution, but seems incomplete (and erroneous because there are two underscore in
__declspec
). If in another module, you will have to import the symbols (with__declspec(dllimport)
and not export them. See the entire solution in this answer -
Mayur about 3 yearswill project reference be sufficient? for visual studio?