0

We have a solution with projects that is generate by cmake.

There is src folder that has all the source code. Proj_Service1, Proj_Service2 and Proj_Service3 reference same Main.cpp and Base_Service.cpp files. In addition, each project has reference to its own ServiceX.cpp file. All projects are console projects (exe). See solution structure below:

-Solution
----Proj_Service1
-------Main.cpp
-------Base_Service.cpp
-------Service1.cpp
----Proj_Service2
-------Main.cpp
-------Base_Service.cpp
-------Service2.cpp
----Proj_Service3
-------Main.cpp
-------Base_Service.cpp
-------Service3.cpp

The reason the structure above in that way is becuase it was decided in the company that each project should include only files that relevant to the project.

I want to add Test_Proj project that will test one of the services or/and all of them using googletest.

When I try to instantiate ServiceX.cpp in Test_Proj, I obviously get linker errors since Proj_ServiceX is not lib and therefore I cannot link to it.

What I thought to do is to create an additional project of type "lib", move Service1.cpp, Service2.cpp and Service3.cpp files to it and make all projects reference it. Basically:

-Solution
----Service_Proj
-------Base_Service.cpp
-------Service1.cpp
-------Service2.cpp
-------Service3.cpp
----Proj_Service1
-------[Reference to Service_Proj]
----Proj_Service2
-------[Reference to Service_Proj]
----Proj_Service3
-------[Reference to Service_Proj]
----Test_Proj
-------[Reference to Service_Proj]

Questions:

  1. Is the solution I suggested above is the only way?
  2. If there is a different, better approach, even the one that requires using other tools, I'll be glad to hear.
theateist
  • 13,879
  • 17
  • 69
  • 109
  • quite common practice is to have two folders in each projects `src` and `tst` – Iłya Bursov Aug 06 '18 at 21:08
  • @IlyaBursov, `Proj_ServiceX` is already a console project. The test is also a console project. How do I add a test_main.cpp to `Proj_ServiceX` project? Can you explain please? – theateist Aug 06 '18 at 22:09
  • you will have test cpp inside of each project, not as separate test one – Iłya Bursov Aug 06 '18 at 22:29
  • But, each of the projects has already `main.cpp` file that has `main(...)` function . How can I add test cpp which also has `main(...)` function? I cannot have 2 `main(...)` functions in same project. – theateist Aug 06 '18 at 22:41
  • 1
    usually you don't test main, but classes – Iłya Bursov Aug 06 '18 at 22:42
  • But I need to run the test and it's done from `main(...)` by using `int main(...) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }`. So, are you suggesting to add those lines to the `main` function of my project? – theateist Aug 06 '18 at 22:47
  • @IlyaBursov, can you please provide a simple example? – theateist Aug 06 '18 at 23:16
  • Making the services a library would allow you to link them to different mains. – Michael Surette Aug 07 '18 at 01:36
  • @MichaelSurette, that's exactly what I suggested in my post. But, IlyaBursov says that it's common practice to put tests within a project instead. Do you have any input on this? – theateist Aug 07 '18 at 02:12
  • https://stackoverflow.com/questions/14446495/cmake-project-structure-with-unit-tests – Iłya Bursov Aug 07 '18 at 12:33
  • @IlyaBursov, in the link you provided they recommend exactly what I suggested to do - to move files to a separate lib project and then reference it to the test project. Otherwise, I'll have to add source files to the test project to compile. – theateist Aug 07 '18 at 14:57
  • @theateist with the only difference - project in terms of cmake, not visual studio project, so you will have single project with two "sub-projects"/folders – Iłya Bursov Aug 07 '18 at 15:02
  • But, every time I want to generate a build for visual studio I'll have to add the files from "src" folder instead of just referecing one lib. I suppose if you use cmake from the beginning it's not a big deal since you already have the list of all cpp files that you want to add in your cmakelist.txt, right? If you don't use cmake and/or don't want to maintain it it makes more sense to create a lib and that's all. Agree? – theateist Aug 07 '18 at 15:11

0 Answers0