0

I have a main project and which there I implement some classes and functionality and also a main.cpp to run everything.

Now I try to add another project to test my main project, so I create another project in the same solution which will be my Unit Test Catch 2 for my original project.

Now I try to reference my original project inside my test project - didn't work. I also try to add in linker input dependencies my whole original folders, the debug folder and the cpp folder - didn't work.

Finally I understand that I try to search for lib file of the Original project to refer inside my Test project but cannot find lib file. Is there another way to link between the two projects so I can call classes and functions inside my Test project and test them?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ItayD
  • 1
  • You'll have to structure your projects so they become easy to test, it is not automatic. Creating a separate library project to store "some classes and functionality" is a wise move. A nice side-effect is that it forces you to think how to distribute code across projects, always matters when the solution becomes big and unit tests especially important. – Hans Passant Dec 09 '21 at 11:41
  • **main.cpp to run everything** That does not sound like a library project. Could you provide detailed steps to create the main project? – Minxin Yu - MSFT Dec 10 '21 at 02:21
  • First of all thanks alot for your comments, i didnt create a library, i just create a project, regular one with header.h and just practice with some functions and one main that runs everything, but than i try to add another project to be a unit test of all the functionality, also just for practice, but when i try to connect between the projects i got a linker errors and did i could not fix them. so i understand me options as you say is to create another Library wich contains all oof my functionality? and i would like to get advise from you... wich library is prefer? static or dynamic? – ItayD Dec 10 '21 at 16:54
  • @ItayD Dynamic libraries were considered to be the better approach most of the time. For your reference : [When to use dynamic vs. static libraries](https://stackoverflow.com/questions/140061/when-to-use-dynamic-vs-static-libraries) and [how to create and use DLL](https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-160) – Minxin Yu - MSFT Dec 13 '21 at 01:37

1 Answers1

0

You can only link to another project if that project is build as a library. Your project is currently build as an application, so you cannot do this, unless you reorganize your project into a library and an application.

If you do not want to this, you can also add the sources files you want to test to your test project. Do not copy the files but reference the existing files in the unit test project.

Right click on "Header files" or "Source files" of your test project in the Solution Explorer, then "Add", "Existing item...". Now these sources files will also by compiled and linked when building your test project.

rveerd
  • 3,620
  • 1
  • 14
  • 30
  • Thank you for your help, is there an option to add an library to my first project wich will contain all the functions/classes and then to link the library to the unit test project? if so, i would like to ask what is more recomended astatic library or adynamic library? and also, what they should contains and how to link them all together. Again thanks alot! Itay. – ItayD Dec 10 '21 at 17:04
  • You should not reorganize your project just for unit test. If you do want to use a separate library, you have to manually do that. Your solution then contains three projects: the library, the main program and the unit tests. – rveerd Dec 13 '21 at 07:22