0

in a MSVC dll project, i tried to create a dll containing a base class

//header file
class MATHLIBRARY_API Shape {
protected:
    int width, height;
public:
    Shape(int, int);
    int area(void) { return -1; };
};

it's compiled successfully, but when adding a virtual specifier to the function

class MATHLIBRARY_API Shape {
protected:
     int width, height;
public:
     Shape(int, int);
     virtual int area(void) { return -1; };
};

the compiler showed error msg

Error   LNK2019 unresolved external symbol `__declspec(dllimport) const Shape::`vftable'" (__imp_??_7Shape@@6B@) referenced in function "public: __thiscall Shape::Shape(int,int)" (??0Shape@@QAE@HH@Z) Dll3    c:\Users\langj\source\repos\Dll3\Dll3\Dll3.obj  1   

where could be the problem?

benkyou
  • 45
  • 9
  • Add a virtual destructor to `Shape`. You are going to need it anyway. – Richard Critten Jun 29 '18 at 09:02
  • You should provide example to reproduce this problem. If `MATHLIBRARY_API` is expanded to `__declspec(dllexport)` then this should work fine (when compiling dll). If error is shown in client code then you probably forgot to recompile dll. – user7860670 Jun 29 '18 at 09:42
  • It may be related to the compiler trying to inline the area() method. The virtual qualifier could conflict with this inlining, especially for a symbol that has to be exported from a dll. Try to move the method implementation in the .cpp file and see if it fixes the problem. – Gabriella Giordano Jun 29 '18 at 10:08
  • Getting a link error that talks about a *dllimport* from a source file named dll3.cpp is not very healthy. That name strongly suggest you are building this DLL, it should therefore be a *dllexport*. Which suggest the MATHLIBRARY_API macro is expanding correctly. If dll3.cpp is not part of this DLL project then you are probably linking an old copy of the .lib file. – Hans Passant Jun 29 '18 at 13:43
  • solved,it's due to that the library exports keyword was not defined. – benkyou Jul 18 '18 at 02:22
  • @benkyou, sorry for resurrecting this old quesion, but I'm hitting a missing vftable too and have not been able to fix it. What do you mean by "the library exports keyword was not defined"? – Axel Feb 08 '22 at 23:22
  • Also, this [answer](https://stackoverflow.com/questions/51098032/compile-error-due-to-virtual-function-in-dynamic-link-library) was helpful, but didn't get me all the way (or I may have overlooked a missing body somewhere, but I really checked) – Axel Feb 08 '22 at 23:23

0 Answers0