0

I have currently two projects, application and static library used by that project. In my static library, I have something along these lines:

// interface.h
struct IInterface {
    virtual ~IInterface() = default;
    virtual void foo() = 0;
};

// baseclass.h
template < class T > struct BaseClass : public IInterface {
    virtual ~BaseClass() = default;
    virtual void foo() override final;
protected:
    virtual void onFoo() = 0;
};

template < class T > BaseClass< T >::foo() {
    // some other code here
    onFoo();
}

// derivedclass.h
struct SomeStruct { /* ... */ };

struct DerivedClass : public BaseClass< SomeStruct > {
protected:
    virtual void onFoo() override final;
}

// derivedclass.cpp
void DerivedClass::onFoo() {
    // ...
}

After I tried to compile this as static library, everything was ok. Then I tried to actually statically link this library to my application and use these classes, and following warnings and errors occurred in Visual Studio 2017:

Warning C4505 ... : unreferenced local function has been removed

which, in turn, caused linker error of unresolved external symbol. It seems that compiler removed foo function. Is there anything wrong with this kind of code? Why does compiler think that my function is local and unreferenced?

Alexander Bily
  • 925
  • 5
  • 18
  • Possible duplicate of [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – 273K Mar 20 '19 at 09:29
  • What does the code that uses your static library look like? Which header(s) does it include, and which functions does it call? Also, which symbol is the "unresolved external symbol" referring to? – Anthony Williams Mar 26 '19 at 09:11

0 Answers0