0

I have a project and want to use google test, I think I followed the steps from this tutorial but it doesn't work.

For example, trying

#include "pch.h"
#include "../../common/units.h"
#include "gtest/gtest.h"

TEST(TestCaseName, TestName) {
    VTile v(17, 16);
    EXPECT_EQ(v.x, 17);
}

leads to an LNK2019: unresolved external symbol "public: __thiscall VTile::VTile(float const &,float const &)" (??0VTile@@QAE@ABM0@Z) referenced in function "private: virtual void __thiscall TestCaseName_TestName_Test::TestBody(void)" (?TestBody@TestCaseName_TestName_Test@@EAEXXZ) tests C:\Users_\Desktop_\src\client\tests\test.obj 1

But this compiles (and the test runs, and fails, which is normal)

#include "pch.h"
#include "../../common/units.h"
#include "gtest/gtest.h"

TEST(TestCaseName, TestName) {
    VTile v;
    EXPECT_EQ(v.x, 17);
}

Why can I include the header and use the default constructor (made VTile() = default in header) but not any methods defined in the .cpp? its like this for all of my files, anything in the header is fine, cpp is not.

Why does it not find the definitions?

Bruno CL
  • 149
  • 9
  • Undoubtedliy this is because you aren't linking your cpp file. Seems simple enough, you can use the functions in the header but you can't in the cpp file, therefore the cpp file isn't being linked into your program. Nothing to do with google test, just a simple linking error. – john Jan 13 '21 at 09:08
  • I see you are using Visual Studio. Did you remember to add the cpp files to your project? – john Jan 13 '21 at 09:14
  • And that is exactly my question... – Bruno CL Jan 13 '21 at 09:15
  • So you need to add the cpp files to the google test project, did you do that? – john Jan 13 '21 at 09:15
  • I don't know how to do that – Bruno CL Jan 13 '21 at 09:16
  • unless you mean literally going with the "add new existing item" option but then I have to update the test project every time I create a file in the main project to test it. – Bruno CL Jan 13 '21 at 09:18
  • If you have an existing project (you didn;t say that before) then what kind of project is it, an executable, a static library, a DLL? – john Jan 13 '21 at 09:20
  • In any case that's the project you need to link to, I'm not totally familar with google test and visual studio, but clearly that the part that is missing, you google test project is not being linked against your main project. – john Jan 13 '21 at 09:23
  • it is an executable – Bruno CL Jan 13 '21 at 09:23
  • Well that seems to be a problem because your unit tests will be an executable as well, and AFAIK you can't link one executable against another. You need to reorganise your code so that your main project and your unit test project both reference a library project which contains the code common to both. – john Jan 13 '21 at 09:25
  • Or you could pick "add existing item" for both projects and have the code compiled separately in each project. – john Jan 13 '21 at 09:26
  • thank you I will make a .lib – Bruno CL Jan 13 '21 at 09:33

0 Answers0