+ 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
1 Respuesta
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