Ausche Ausche -4 years ago 91
C++ Question

Sharing classes within one solution

I'm working on a utility (for practice) that has two tools that a user can run.

I want to know if there's an outer layer within a solution where I can build classes that are recognized by all projects of said solution.

I'm at the point where both tools are finished and I want to add the two projects to one solution. However, these tools can share a few non-static classes and I really want to avoid having multiple of the same .cpp/.h files for each project so if I need to edit or add to a shared class, I don't have to copy/paste the edits into each project.

I tried using resource files, but they won't add .h or .cpp files. I tried adding the classes to their own project and then using them as references in the other projects, but the classes within the other projects won't recognize them. I also looked around at creating a library, but I'm not sure if it's possible to create non-static libraries as these projects will have multiple objects of the shared classes.

I'm very visual and I'm not sure if I explained my issue well so here's a simple diagram of what I want. Each arrow shows who each project can "be aware of" so-to-speak (conceptually similar to class inheritance). The bold First Project is the solution's entrance; essentially just where the user specifies which tool to run.

File structure diagram

Answer Source

From what you described a class library would be the solution. This would allow you to share your two classes between both projects. In C++ there are two types of class libraries the Static Link Library and the Dynamic Link Library.

Here is a nice answer from a previous StackOverflow question which should aid you in determining which type of class library to use.

I have also included two separate links from Microsoft, since you tagged your post with Visual Studio, on how to create and use a library of each type.

MSDN: Static Link Library Tutorial

MSDN: Dynamic Link Library Tutorial

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download