How to export pure virtual functions from a DLL in C ++?

I had a strange problem: there is no pure virtual function exporting from a DLL. The DLL compiles and displays the DLL file in a directory. But it does not create a .lib file.

If I give a definition and it is no longer remy as pure virtual, then it creates a .lib file.

I need to implement a factory template, for which I need to separate interfaces and implementations. My factory implementations and other interfaces that use the required .dll (they donโ€™t create from any .lib file) should use this exported function, and when I use these functions, they create binding errors ...

for example, "error LNK2011: unresolved external character" public: ....... "

Is there any idea how to export pure virtual functions so that they can be implemented for other exe and dll

Usman Relations

+4
source share
3 answers

When you export something from a DLL, you create an externally visible name for something specific in that DLL - a specific function or class. Without this, the link step for importing projects (those that reference this DLL) cannot resolve all the necessary function and class references in the exporting DLL.

For pure virtual functions, the exporting DLL does not have a specific โ€œthingโ€: to resolve an external call to a pure virtual function, there is no middle name โ€” if it were, then by definition it would not be clean. In this case, all that is required is a declaration of a pure virtual function in the header file with pure information, so importing an EXE or DLL knows how to override it with a specific function.

+6
source

In C ++, you can define a pure virtual method. For instance:

// T.hpp class T { public : virtual void doSomething() = 0 ; // etc. } ; 

.

 // T.cpp void T::doSomething() { } // etc. 

Now that the dllexport / dllimport specifier is added to the T class, you will choose the doSomething method.

The point of determining the body of a pure virtual method was to make sure that the user overrides this method, while maintaining the default implementation.

My own use of this template is to avoid crashing when for some reason the virtual method is called before it exists or when it no longer exists (i.e. in the constructor or destructor of the base abstract class). In debug mode, it will launch the debugger ( ::DebugBreak() function of the Win32 API), and in release mode it will do nothing.

But this template can also be used to solve your problem if you really need to export your pure virtual functions.

+1
source

All Articles