Unresolved external symbol, cannot figure out why - c++

I have two files that are causing me a lot of grief: camAVTEx.h and camAVTEx.cpp. Here is the general setup for the two files:
//.h////////////////////////////////////////////////
/*
#includes to some other files
*/
class camera_avtcam_ex_t : public camera_t
{
public:
camera_avtcam_ex_t();
virtual ~camera_avtcam_ex_t();
private:
//some members
public:
//some methods
};
void GlobalShutdownVimbaSystem();
//.cpp/////////////////////////////////////////////
#include "StdAfx.h"
#include "camAVTEx.h"
//some other #includes
camera_avtcam_ex_t::camera_avtcam_ex_t()
{
}
//rest of the class' functions
void GlobalShutdownVimbaSystem()
{
//implememtation
}
Then, in a file in a different directory, I do a #include to the exact location of the .h file and try to use the class:
//otherfile.cpp
#include "..\..\src\HardSupport\Camera.h"
//this is the base camera class (camera_t)
#include "..\..\src\HardControl\camAVTEx.h"
//this is indeed where both the .h and .cpp files are located
void InitCam
{
camera_t* maincam = new camera_avtcam_ex_t();
}
void OnExit()
{
GlobalShutdownVimbaSystem();
}
When I compile, I get the following errors:
8>otherfile.obj : error LNK2001: unresolved external symbol "public: __cdecl
camera_avtcam_ex_t::camera_avtcam_ex_t(void)" (??0camera_avtcam_ex_t##QEAA#XZ)
8>otherfile.obj : error LNK2001: unresolved external symbol "void __cdecl
GlobalShutdownVimbaSystem(void)" (?GlobalShutdownVimbaSystem##YAXXZ)
8>....\bin\x64\Release\otherfile.exe : fatal error LNK1120: 2 unresolved externals
I cannot for the life of me figure out why it can't find the implementations for these two functions.
So I guess my question is fairly obvious: Why am I getting these errors and what do I need to change to fix them?

Whatever how you look at it, the error you have : unresolved external symbol "public: __cdecl camera_avtcam_ex_t::camera_avtcam_ex_t(void)" (??0camera_avtcam_ex_t##QEAA#XZ) means that the compiler knows the symbol camera_avtcam_ex_t::camera_avtcam_ex_ (that's the class constructor) since he saw its declaration in the camAVTEx.h file but halas, it can't find (= resolve) the implementation of this symbol (in short, the code).
This usually happen because of several possible causes :
you didn't tell the compiler about the code (.cpp) you try to use so he doesn't know it. Try to add the file to your project.
you compile the missing code, but don't link with it. Check if you don't have two separated projects or try to add the lib to your project if it comes from a lib.
in some way, the compiled code does not match its definition (happens when mixing C and C++ or messing with namespaces) Check if you are not declaring contradicting enclosing namespaces.
(maybe other reasons I don't know ?)

Related

Unresolved Add_Test symbols, even with test fixture class included in the file

All of a sudden, after my tests had been working for hours, I'm getting errors that it can't find my text fixture functions (SetUp/TearDown). I'd had the fixture defined in a separate file, so as a quick fix I moved the class to the test file, but I still get the same problem!
Here's the current file. I didn't just omit the code for brevity, I've tried building it in this form to make sure the problem isn't somehow caused by the code inside the tests. I've left in the includes though.
#include "memory"
#include "../TestUtility/TestUtility.h"
#include "DependencyInjector.h"
#include "gtest/gtest.h"
#include "RecoverableErrorException.h"
namespace Test
{
class DependencyInjectorTest : public testing::Test
{
public:
DependencyInjectorTest();
virtual ~DependencyInjectorTest();
protected:
virtual void SetUp() { Framework::DependencyInjector::Destroy(); };
virtual void TearDown() override;
};
TEST_F ( DependencyInjectorTest, FindEmpty )
{
// content ommitted
}
TEST_F ( DependencyInjectorTest, Add )
{
// content ommitted
}
TEST_F ( DependencyInjectorTest, Find )
{
// content ommitted
}
} // namespace Test
And here are the errors:
1>DependencyInjectorUnitTests.obj : error LNK2019: unresolved external symbol "public: __thiscall Test::DependencyInjectorTest::DependencyInjectorTest(void)" (??0DependencyInjectorTest#Test##QAE#XZ) referenced in function "public: __thiscall Test::DependencyInjectorTest_Add_Test::DependencyInjectorTest_Add_Test(void)" (??0DependencyInjectorTest_Add_Test#Test##QAE#XZ)
1>DependencyInjectorUnitTests.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Test::DependencyInjectorTest::~DependencyInjectorTest(void)" (??1DependencyInjectorTest#Test##UAE#XZ) referenced in function "public: virtual __thiscall Test::DependencyInjectorTest_Add_Test::~DependencyInjectorTest_Add_Test(void)" (??1DependencyInjectorTest_Add_Test#Test##UAE#XZ)
1>DependencyInjectorUnitTests.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall Test::DependencyInjectorTest::TearDown(void)" (?TearDown#DependencyInjectorTest#Test##MAEXXZ)
UPDATE: The errors disappeared at some point. And then a few hours later, just as mysteriously and frustratingly, came back. Even on files I hadn't worked on since it was fine.
You need to define the constructor, destructor and TearDown methods of DependencyInjectorTest. Even if they are virtual, the compiler needs basic methods to initialize the vtable.
https://en.cppreference.com/w/cpp/language/virtual
By declaring them in the class definition, the macros used them implicitly, and they need to be defined. As they're not, linking results in an undefined symbol error.
You can either remove the declarations or define the functions.

How to fix C++ linker error with no explaination

I have a C++ project that won't compile, and the following 2 errors are produced:
Error LNK1120 1 unresolved externals
Error LNK2019 unresolved external symbol "public: virtual __cdecl
StartWindow::~StartWindow(void)" (??1StartWindow##UEAA#XZ) referenced
in function "public: void __cdecl StartWindow::`vbase
destructor'(void)" (??_DStartWindow##QEAAXXZ)
StartWindow is a class I have defined, but currently it is never instantiated or included anywhere in the project. Deleting the class allows the project to compile, but if this class is within the project it won't.
I will include the code for the class in case I am missing something:
.CPP File
#include "StartWindow.h"
StartWindow::StartWindow()
{
setImmediateDrawMode(false);
}
void StartWindow::onDraw()
{
clearScreen(WHITE);
EasyGraphics::onDraw();
}
Header File:
#pragma once
#include "EasyGraphics.h"
class StartWindow : public EasyGraphics
{
public:
StartWindow();
~StartWindow();
private:
virtual void onDraw();
};
Thanks.
You're missing the implementation for the destructor for StartWindow. In your implementation file (.cpp file), append:
StartWindow::~StartWindow(){
//if your destructor is non-trivial, include definition here
}

MSVC++ Linker error which I do not quite understand [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
Right I am getting weird linker errors which I've never seen before and which I can't really decipher.
Code for the RelationOwner and RelationUser can be found there. One remark: I've moved all function bodies to the source file instead of the header file. Naming has stayed the same.
Error 1 error LNK2019: unresolved external symbol "public: __thiscall RelationUser<class Family,class Citizen>::RelationUser<class Family,class Citizen>(class Family *)" (??0?$RelationUser#VFamily##VCitizen####QAE#PAVFamily###Z) referenced in function "public: __thiscall Citizen::Citizen(class Family &,class Name)" (??0Citizen##QAE#AAVFamily##VName###Z) *path*\Citizen.obj CodeAITesting
Error 2 error LNK2019: unresolved external symbol "public: __thiscall RelationUser<class Family,class Citizen>::~RelationUser<class Family,class Citizen>(void)" (??1?$RelationUser#VFamily##VCitizen####QAE#XZ) referenced in function __unwindfunclet$??0Citizen##QAE#AAVFamily##VName###Z$1 *path*\Citizen.obj CodeAITesting
Error 3 error LNK2019: unresolved external symbol "public: __thiscall RelationOwner<class Family,class Citizen>::~RelationOwner<class Family,class Citizen>(void)" (??1?$RelationOwner#VFamily##VCitizen####QAE#XZ) referenced in function __unwindfunclet$??0Family##QAE#VName###Z$1 *path*\Family.obj CodeAITesting
The first one is about a constructor and the second two are about the destructor. That I understand.
I've also implemented my own version of the User and Owner as the following:
// header of Family.h (Owner in the linked PDF file)
#include "Name.h"
#include "RelationOwner.h"
class Citizen;
class Family; // I didn't really know if this one was necessary.
class Family
: public RelationOwner<Family, Citizen>
{
public:
Family(Name name);
private:
Name name;
};
// Source of Family.cpp
#include "Name.h"
Family::Family(Name name)
: name(name)
{
}
//Source of Citizen.h (User in the linked PDF)
#include "Name.h"
#include "RelationUser.h"
class Citizen;
class Family;
class Citizen
: public RelationUser<Family, Citizen>
{
public:
Citizen(Family &family, Name name);
private:
Name name;
};
// Source of Citizen.cpp
#include "Family.h"
#include "Name.h"
#include "RelationUser.h"
Citizen::Citizen(Family &family, Name name)
: RelationUser<Family, Citizen>(&family),
name(name)
{
}
As far as I know I am not doing anything fancypancy, yet it is complaining big time and I don't know why.
Well as Brian Beuning told me "usually the code for a template is in the header file".
And after reading n.m.'s linked possible duplicate I moved the implementation back to the header file and, it worked.
I guess that the duplicate question was spot on.

Linker can't find function definitions, LNK2001 unresolved external symbol

Here is my simple setup: (i've hidden lots of unneeded information)
//AutoFocusTest.h
#include "camAVTEx.h"
class CAutoFocusTestApp : public CWinApp
{
protected:
camera_t* mCamera;
public:
virtual BOOL InitInstance();
};
//camAVTEx.h
class camera_avtcam_ex_t : public camera_t
{
public:
camera_avtcam_ex_t();
virtual ~camera_avtcam_ex_t();
//member variables
//member function declarations
}
//camAVTEx.cpp
#include "camAVTEx.h"
camera_avtcam_ex_t::camera_avtcam_ex_t()
{
//stuff
}
camera_avtcam_ex_t::~camera_avtcam_ex_t()
{
//stuff
}
//the rest are defined here in my program
//AutoFocusTest.cpp
#include AutoFocusTest.h
BOOL CAutoFocusTestApp::InitInstance()
{
mCamera = new camera_avtcam_ex_t();
}
This setup produces the error:
3>AutoFocusTest.obj : error LNK2001: unresolved external symbol
"public: __cdecl camera_avtcam_ex_t::camera_avtcam_ex_t(void)"
(??0camera_avtcam_ex_t##QEAA#XZ)
From everything I've read on this relatively common problem, I have not linked something causing my camera_avtcam_ex_t function definitions to not be found. However, I can't figure out what I could have missed. I have added all of the include directories and library directories, as well as added the library files to the additional dependencies section.
Can anyone spot anything that I might be missing?
Assuming you have defined the constructor for your camera_avtcam_ex_t, it's declared as private, you can't instantiate it.

C++ LNK2019 error with constructors and destructors in derived classes

I have two classes, one inherited from the other. When I compile, I get the following errors:
Entity.obj : error LNK2019: unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)" (??0Base#Parsables#Utility##QAE#XZ) referenced in function "public: __thiscall Utility::Parsables::Entity::Entity(void)" (??0Entity#Parsables#Utility##QAE#XZ)
Entity.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Utility::Parsables::Base::~Base(void)" (??1Base#Parsables#Utility##UAE#XZ) referenced in function "public: virtual __thiscall Utility::Parsables::Entity::~Entity(void)" (??1Entity#Parsables#Utility##UAE#XZ)
D:\Programming\Projects\Caffeine\Debug\Caffeine.exe : fatal error LNK1120: 2 unresolved externals
I really can't figure out what's going on.. can anyone see what I'm doing wrong? I'm using Visual C++ Express 2008. Here are the files..
"include/Utility/Parsables/Base.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_BASE_HPP
#define CAFFEINE_UTILITY_PARSABLES_BASE_HPP
namespace Utility
{
namespace Parsables
{
class Base
{
public:
Base( void );
virtual ~Base( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_BASE_HPP
"src/Utility/Parsables/Base.cpp"
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
Base::Base( void )
{
}
Base::~Base( void )
{
}
}
}
"include/Utility/Parsables/Entity.hpp"
#ifndef CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#define CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#include "Utility/Parsables/Base.hpp"
namespace Utility
{
namespace Parsables
{
class Entity : public Base
{
public:
Entity( void );
virtual ~Entity( void );
};
}
}
#endif //CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
"src/Utility/Parsables/Entity.cpp"
#include "Utility/Parsables/Entity.hpp"
namespace Utility
{
namespace Parsables
{
Entity::Entity( void )
{
}
Entity::~Entity( void )
{
}
}
}
The relevant bit is this:
unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)"
You need to provide a definition for Base::Base and Base::~Base. A declaration is not good enough. Even if you have nothing to do in either function, you need to leave an empty function body, because C++ actually requires the function to exist. C++ puts things like virtual table maintenance inside your constructors and destructors, so they must be defined even if you don't need to do anything there -- C++ has to do things in there.
Are you sure Base.cpp is being included in the build?
Just encountered this exact same error today in Visual Studio 2015. Unfortunately the accepted answer didn't worked (as well as answers from many same questions). The thing that worked for me was right click on the base class cpp file, exclude and then include it again. I think somehow VS got confused while moving file around and renames and it just silently refused to compile it even though it was marked as "Included In project" = true in property editor as well as listed in vcproj file in group. This is horrible error and ended up spending good hour on it.
Either your base.cpp is not being compiled/linked or you have a misspelling in it