2

I have a solution with two projects in it

One of them is a console application, and the other one is a Google Test project

My project has a .h file and a .CPP with a main() in it

My gtest consists of a .CPP file which calls the .h file using #include and a main function to RUN_ALL_TESTS()

I need a main in my project but I also need a main in the gtest project, but having two main() doesn't let me build the gtest successfully

Is there a workaround to this? Sorry if it's a silly question, I have no clue how to use gtest because various sites keep presenting different ways

lmnml
  • 37
  • 5

3 Answers3

1

First of all you should have a dedicated file main.cpp for your main() function, which contains nothing else.

E.g. your project structure could look like:

  • project1
    • file1.h
    • file1.cpp
    • main.cpp

I'm not familiar wiht gtest specifically, but usually unit test frameworks have a separate file for the gtest main function, e.g. gtest_main.cpp. Tests are in one or more files like file1test.cpp etc.

So you would compile and link your project1 with file1.h, file1.cpp and main.cpp to get an executable.

For unit tests you would compile and link file1.h, file1.cpp, file1test.cpp and gtest_main.cpp for a unit test executable.

Structure could be like

  • project1
    • file1.h
    • file1.cpp
    • main.cpp
  • project1test
    • file1test.cpp
    • gtest_main.cpp

EDIT additional infos on linking:

In project1test you would include file1.h with #include "../project1/file1.h".

For correct linking right-click on project1test project

--> Configuration Properties --> Linker --> Input --> Additional Dependencies --> Add "..\project1\Debug\file1.obj"

As @Alan Birtles pointed out it would be even more clearer if you had the following structure:

  • project1library
    • file1.h
    • file1.cpp
  • project1application
    • main.cpp
  • project1test
    • file1test.cpp
    • gtest_main.cpp

The you would get a static/dynamic library project1library.lib/.dll, an executable project1application.exe and a unit test executable project1test.exe.

The advantage is that you would just link the library in your unit test project with

--> Configuration Properties --> Linker --> Input --> Additional Dependencies --> Add "..\project1library\Debug\project1library.lib"

If you have more than one file you need from your project, you don't have to add every obj file, but just one lib file.

But making sure that everything was rebuilt correctly on changes can be more difficult and error prone with a lib, an executable and a unit test project.

Simon
  • 1,616
  • 2
  • 17
  • 39
  • Don't think this will work, linking project1 into project1test will result in 2 definitions of main, you need 3 projects, one for the library, one for tests and a third for the application which contains main – Alan Birtles Jan 20 '20 at 08:30
  • If you just link file1.cpp and NOT main.cpp, it would work. However, you are right that different directories/projects for library, application, test provides a clearer separation, but makes it also more complicated. – Simon Jan 20 '20 at 08:33
  • Sorry for the rookie question, but how do I link gtest_main.cpp with file1.h and file1.cpp in Visual Studio 2017? They belong to different projects. – lmnml Jan 20 '20 at 08:36
  • Thank you. I didn't think splitting the main.cpp from the rest of the project. I made a .lib using file1.h, file1.cpp and main.cpp, makes sense why it didn't work. Just one last doubt, is this the standard way in which C++ unit testing is done (splitting the main from the other files), or is there another proper way? – lmnml Jan 20 '20 at 09:58
0

Standart usage of gtest is for unit testing. Usually, unit tests don't check main :).

I recommend you to use standart gtest main function (don't define custom main function). it allows you to use command line to filter running tests.

If you don't want to use gtest main, IMHO, you shouldn't include gtest_main library.

Evgeny
  • 1,072
  • 6
  • 6
  • I haven't manually included any library, I'm using VS2017, Google Test comes as a package with it. If I don't write a main() with RUN_ALL_TESTS() in it, then the test explorer is unable to find my tests. – lmnml Jan 20 '20 at 08:22
0

I used macros for this problem. I have defined a TESTING macro which evaluates to true when compiling the unit tests and otherwise to false:

#ifndef TESTING
// the source main
int main() {
    ...
}
#endif // !TESTING

You can also use this later for "test" code in your sources. What I do sometimes (not good design IMO):

class Klass:

#ifdef TESTING
    friend class KlassUnitTestClass; // allows access to private members in my google test unit class. Disabled when i build sources
#endif // !TESTING
RoQuOTriX
  • 2,871
  • 14
  • 25