Search code examples
c++visual-studio-2012inheritancelnk2019

C++ inheritence and LNK2019


I have got this problem and don't know how to solve it.

I have two classes. One is base ProgramVariableBase and one is derived ProgramVariable. Base class is in different project that is bulid as static library. I set additional include and library directories in properties of project with derived class. I am using VS2012.

// ProgramVariableBase.h
#include <string>

class ProgramVariableBase
{
    protected:
        std::string m_name;
        bool m_initialized;

    public:
        ProgramVariableBase(const char* name);
        virtual ~ProgramVariableBase();
        virtual const std::string & Name();
        virtual void MakeInitialized();     
};

// ProgramVariableBase.cpp
#include "ProgramVariableBase.h"

ProgramVariableBase::ProgramVariableBase(const char* name)
{ 
    m_name = name;
    m_initialized = false;
}

ProgramVariableBase::~ProgramVariableBase()
{ 

}

const std::string & ProgramVariableBase::Name() 
{ 
    return m_name; 
}

void ProgramVariableBase::MakeInitialized()
{
    m_initialized = true;
}

// ProgramVariable.h
#include "ProgramComponents\ProgramVariableBase.h"

class ProgramVariable : public ProgramVariableBase
{
    public:
        ProgramVariable(const char* name);
        ~ProgramVariable();

        void MakeInitialized() override;

};

// ProgramVariable.cpp
#include "ProgramVariable.h"

ProgramVariable::ProgramVariable(const char* name) : ProgramVariableBase(name)
{ 
}

ProgramVariable::~ProgramVariable()
{ 
}

void ProgramVariable::MakeInitialized()
{
    m_initialized = true;
}

I can build project with base class with no problem, but I get errors when I try build project with derived class. Every error looks similar and concerns methods from base class, its constructor and destructor. I looked for similar answers here, but no one helped. Am I doing something wrong or is my project properties incorrect?

unresolved external symbol "public: __thiscall ProgramVariableBase::ProgramVariableBase(char const *)" (??0ProgramVariableBase@@QAE@PBD@Z) referenced in function "public: __thiscall ProgramVariable::ProgramVariable(char const *)"

unresolved external symbol "public: virtual __thiscall ProgramVariableBase::~ProgramVariableBase(void)" (??1ProgramVariableBase@@UAE@XZ) referenced in function "public: virtual __thiscall ProgramVariable::~ProgramVariable(void)" 

Solution

  • You need to add the library to your linker settings. Just putting the folder containing the library into your additional library settings is not sufficient.

    You can add the library to your linker settings via Linker->Input->Additional Dependencies or a use a pragma in your c++ code.

    #pragma comment(lib, "mylib.lib")