If you meet LNK2019 Problem
Reason: you add wrong lib.
Solution: only add gtestd.lib (and gtest_maind.lib, if don’t want to write main function by yourself).
Tutorial
In short
download gtest and gmock form here.
compile them into libraries and link your test with it (choose file according to your develop environment, msvc for visual studio).
add profiles contains .h file
Detail
1. Gtest
Download gtest(and gmock, if you need use its mock function).
github clone project from here.
open gtest.sln.
build all under Debug( and release) mode [I don’t think we only need dug version]. Then we can get (gtestd.lib and gtest_maind.lib) for debug, (gtest.lib and gtest_maind.lib) for release.
copy four of them in a lib profile(I save it under include).
setting in your test project.
add lib dependencies.
[ properties -> linker -> general ]
add lib (for debug)
Attention: only add (gtestd.lib and gtest_maind.lib), if you only want debug.(otherwise, it will has LNK2038 problem)
add additional include directories
– [ properties -> c/c++ -> additional include directories ]
– Thus, we could write #include “gtest\gtest.h”.( you also need to add address to “gmock\gmock.h”, if you want use gmock).
change runtime library.
– MTd for debug; MT for release.
Write a test demo
#include <tchar.h>
#include <gtest/gtest.h>
int Foo(int a, int b)
{
if (a == 0 || b == 0)
{
throw "don't do that";
}
int c = a % b;
if (c == 0)
return b;
return Foo(b, c);
}
TEST(FooTest, HandleNoneZeroInput)
{
EXPECT_EQ(2, Foo(4, 10));
EXPECT_EQ(6, Foo(30, 18));
}
int _tmain(int argc, _TCHAR* argv[])
{
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
system("pause");
return 0;
}
2. Gmock
Install process is similar to gtest. It only can generate one version lib(gmock.lib), just add it to project.