• 13
• 14
• 27
• 9
• 9

# share a class among multiple dll's

This topic is 4449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a main application project that defines class A. And i have several DLL projects (that will link to the main application) that make use of class A. How does A get shared among projects? Currently im just adding the class into each dll project workspace (Project->Add Existing Item->A.h A.cpp) - but im sure this is not correct. Does a have to be recompiled and linked for each dll?

##### Share on other sites
Hello,

First off I apologize if I am misunderstanding your question, but here is what I think you are looking for. (I assume you are using VC)

In any projects that are going to use the DLL define the following:
(Use a better naming convention as this is just an example)
#if defined(SOME_API)#define CAPI __declspec(dllexport)#else#define CAPI __declspec(dllimport)#endif

For the main DLL (The one with Class 'A') go to Project->Settings and click the C/C++ tab. Under "Preprocessor Definitions" add "SOME_API" to the list. This will use __declspec(dllexport) which will allow you to export the class. (Note that this needs done for both debug and release builds)

Where you define class 'A' define it like the following:
class CAPI A{    ...};

Then compile the DLL.

Now that you have your DLL just include the "a.h" to any project you want to use the DLL in. Do not add anything to the preprocessor definitions so it will use __declspec(dllimport) and inport the class.

That's the whole thing in a nut shell. I have a meeting in a few minutes or I would explain a little better.

Best of luck.