0

I am quite new to both C++ and Visual Studio. I’ve got a solution with one project, which is my application. Now I’ve got to write some C++ classes that form a functional unit. I want to put them separately, probably in their own project (Anything else like “packages” do in Java would be sufficient. Unluckily, I only find myself in Visual Studio with three virtual folders (headers, resources, classes) which even map to the same folder on disk. At least, I would wish to visually separate the classes with headers in the front-end, if the same happens on file system level it would be fine.) and I want to be able to unit-test them from Visual Studio’s test running interface.

The MSDN tutorial to set up a unit test assumes a DLL being tested. As per comment, it isn’t absolutely necessary to make a DLL for this. Putting classes in DLLs seems to complicate things by a lot. I do not need to load the machine code dynamically and I would like to use my classes as easy as if they were in my application project. Just I want to have it separate somehow and I want write unit-tests for them which should also be separated from both my main program and from the code under test. How can I set this up in a simple manner?

Community
  • 1
  • 1
Matthias Ronge
  • 9,403
  • 7
  • 47
  • 63
  • Using a DLL isn't essential. The test projects I'm currently working with use static libs (which are much simpler to make and use than DLLs) but I'm pretty certain you can just make a project containing all the appropriate source files along side your test definitions and build it all as one single blob. – Rook May 19 '17 at 17:44
  • (also, you're asking two separate questions here; either move the how-do-i-dll stuff into a separate question or just delete it) – Rook May 19 '17 at 17:45
  • "Putting classes in DLLs seems to complicate things" it doesn't. False premise, invalud conclusion. Doesn't mean you have to use a DLL though. – n. m. could be an AI May 22 '17 at 09:39
  • Use static libraries https://en.wikipedia.org/wiki/Static_library – user3811082 May 22 '17 at 09:40
  • @n.m. As per the linked question it does, because the `new` operator cannot be used anymore (in [this](http://stackoverflow.com/a/24384693/1503237) answer) – Matthias Ronge May 22 '17 at 09:43
  • @user3811082 Is there a tutorial for static library (where to click, including the test setup) that I could follow? I wonder: Does it have any implications? Likewise: what do my `#include` statements have to look like? Do I have to edit (compiler/linker) settings? If so, which? How do I get towards a minimal example that both compiles and executes (the main program) and (at least an empty) test runs in VS? – Matthias Ronge May 22 '17 at 09:53
  • "the new operator cannot be used anymore" The answers you link doesn't say anything like that, and if it did it would be dead wrong, The answer shows how to export an *interface* via a factory function. You don't have to do this. – n. m. could be an AI May 22 '17 at 11:05
  • @Paramaeleon The problem you might be thinking of relates to allocating in one DLL (or exe) and de-allocating in another when the run-time libraries and compiler parameters are not the same. If you are building everything and have full control, you *can* do this. – crashmstr May 22 '17 at 12:08
  • @crashmstr and of course it doesn't matter whether you are allocating a class type object or an integer in this manner, the consequences are the same. – n. m. could be an AI May 22 '17 at 15:27