How to use shared dll with common header | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to use shared dll with common header

Hi I have a header file which has a class having one pure virtual function. This class is inherited and implement a virtual function. This implemented class ia to be there in a dll project. How to export this function as I am facing issue to write extern "c" __declspec(dllexport) due to virtual I believe. How to use virtual function implementation from shared dll to a different project ? Is there any other way or Am i missing something ?

1st Apr 2023, 5:46 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 1
To export the virtual function from your DLL, you can follow these steps: In your header file, define your class with the virtual function as follows: class MyClass { public: virtual ~MyClass() {} virtual void myFunction() = 0; }; In your DLL project, define the class that implements the virtual function and export it using __declspec(dllexport): class MyImpl : public MyClass { public: virtual void myFunction() override; }; extern "C" __declspec(dllexport) MyImpl* CreateMyImpl() { return new MyImpl; } void MyImpl::myFunction() { // Implementation here } In the client project that will use the DLL, include the header file with the class definition and declare the virtual function as __declspec(dllimport): class __declspec(dllimport) MyClass { public: virtual ~MyClass() {} virtual void myFunction() = 0; }; MyClass* ptr = CreateMyImpl(); ptr->myFunction(); This way, the virtual function implementation will be loaded from the DLL at runtime, and you can use it in the client project.
1st Apr 2023, 6:22 AM
Knight
Knight - avatar
0
Thanks but I am getting into linker error. I tried below: commonHeader_Exp.h has following: #pragma once class myType { public: virtual ~myType() {} virtual void display() = 0; }; commonHeader_Imp.h has following: #pragma once class __declspec(dllimport) myType { public: virtual ~myType() {} virtual void display() = 0; }; Project related to DLL is DLL_A.sln. DLL_A.sln has header.h and Source.cpp. header.h has following: #pragma once #include "d:\SelfLearning\LinkingTrial\commonHeader_Exp.h" class myTypeImpl : public myType { public: void display(); }; Source.cpp has following: #include "pch.h" #include "Header.h" #include <iostream> extern "C" __declspec(dllexport) myTypeImpl * getObject() { return new myTypeImpl; } void myTypeImpl::display() { std::cout << "Display from myTypeImpl of DLL A\n"; } DLL project compiles fine. Project related to client is client.sln and it has client.cpp. client.cpp has following: #include <iostream> #include "d:\SelfLearning\LinkingTrial\commonHeader_Imp.h" #include "d:\SelfLearning\LinkingTrial\DLL_A\DLL_A\Header.h" int main() { myTypeImpl* pMyImpl; pMyImpl->display(); return 0; } It does not compile. If I exclude commonHeader_imp.h from client, it is ok then but how to write those import related class in client.cpp? Also with exclusion of commonHeader_imp.h, it fails to recognize gettype if done as below: myTypeImpl* pMyImpl = getObject(); Again thanks a lot for your help..!
1st Apr 2023, 9:55 AM
Ketan Lalcheta
Ketan Lalcheta - avatar