1

I'm trying to test my simple class for building (that it has good constructor).
File TestClass.h

#pragma once

class TestClass
{
public:
    TestClass();
};

File TestClass.cpp

#include "TestClass.h"

TestClass::TestClass()
{
}

And then I add new project in my solution: tests. This project contains only one file.
File test.cpp

#include <gtest/gtest.h>
#include "../Example/TestClass.h"

TEST(Test0, all)
{
    EXPECT_EQ(true, true);
}

/*
TEST(Test1, part1)
{
    TestClass t;
}*/

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

If I build that code - all is good (and all tests). But if I uncomment commented block - I have such output with error:

1>------ Rebuild All started: Project: Example, Configuration: Debug Win32 ------
1> TestClass.cpp
1> Example.vcxproj -> d:\Alex\documents\visual studio 2015\Projects\tests\Debug\Example.lib
2>------ Rebuild All started: Project: tests, Configuration: Debug Win32 ------
2> test.cpp
2>test.obj : error LNK2019: unresolved external symbol "public: __thiscall TestClass::TestClass(void)" (??0TestClass@@QAE@XZ) referenced in function "private: virtual void __thiscall Test1_part1_Test::TestBody(void)" (?TestBody@Test1_part1_Test@@EAEXXZ)
2>d:\Alex\documents\visual studio 2015\Projects\tests\Debug\tests.exe : fatal error LNK1120: 1 unresolved externals
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========

I added gtest.lib file to dependency of linker input of tests project.
I added Example project as a dependency of tests project.
I built Example project as Static library (.lib).
If I delete constructor from TestClass - all will be good.
My project you can find here.
I'm using Microsoft Visual Studio 2015 with Update 3 on Windows 10 Pro x64.
My Google Test Framework version is 1.8.0.

How can I solve this compilation problem? I really need this constructor.

AJIOB
  • 329
  • 5
  • 12

2 Answers2

2

It's exactly the problem I encounted now. Solution is as follow:

  • Make sure you add the Reference: under Test Project, add reference to the Source Project.

  • Set the Configuration Properties of the project contains TestClass.cpp and TestClass.h as Static library: In VS 2015, it's under Project -> Properties -> Configuration Properties -> General -> Project Defaults -> Configuration Type. Besides, comment out the main function in Source Project.

Hope it helps.:)

Moon Zoe
  • 53
  • 4
0

I can solve this problem:
I manually add buildDir (to me it was $(SolutionDir)$(Configuration)\) to that way:
tests properties -> Configuration Properties -> Linker -> General -> Additional Library directories
And I manually add Example.lib dependency to that way:
tests properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies

AJIOB
  • 329
  • 5
  • 12