Working with Gtest and Gmock on Windows

2.5k views Asked by At

I have a problem with starting using Gtest and Gmock on Windows10. I normally work on Linux but wanted to switch to Windows and encountered a problem.

I managed to build the Gmock and Gtest according to the documentation (with Cmake and Visual Studio). I have the gmock.lib and gtest.lib files in apropriate directories. But when I want to link them to my test program I can see dozens of errors of type "undefined reference".

The command I use is:

g++ "-LC:\\Users\\jacek\\cpp\\googletest\\googlemock\\Debug" "-LC:\\Users\\jacek\\cpp\\googletest\\googlemock\\gtest\\Debug" -o HelloWorld "src\\counter.o" "src\\counter_test.o" -lgmock -lgtest

The errors are for example:

C:\Users\jacek\workspace\HelloWorld\Debug/../src/counter_test.cpp:14: undefined reference to testing::Message::Message()' C:\Users\jacek\workspace\HelloWorld\Debug/../src/counter_test.cpp:14: undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'

I tried searching on this and found that maybe I should build shared libraries. I rebuilt the gmock and gtest with appropriate option, now I have also dll files, but I receive still the same errors.

What am I doing wrong here?

Can it be an issue that the libraries on linux are named differently than on windows? (libgmock.a vs gmock.lib) Should I change something in the command to g++ due to that?

The paths I pasted are good, the files are there (otherwise the error would be different).

What am I missing here?

2

There are 2 answers

0
YotKay On

Ok, I solved the problem. It turned out that I cannot compile libraries with Visual Studio and then use it with G++. I had to switch to a different approach and this video was very helpful: https://www.youtube.com/watch?v=y9sGAF1k63o&list=PL0SUKxlBaq1COi52nuq2lPf6AbMRrn2LI&index=7

The video teaches how to use gtest with Eclipse but not using library but fused cpp and h files from gtest script. It works fine. The only problem I had is that I installed python 3.5 version and it is not backwards compatible in some cases with python 2.7 used in the 'fuse' script, but I managed to quickly correct these few cases and make it work.

0
Soeren On

in addition to your correct answer: if you want to work with external libraries (like gmock or gtest), you have to compile the libraries AND your program with the same compiler. And that is why your program throws the error with the undefined reference. In visual studio there is the MSVC compiler. If you want to work with VS, then just compile your program also with VS. And that's not all, you have to use the same compiler and the same settings (for example compiling in debug mode)

I'm just wanted to add this to your answer. Because of you wrote:

It turned out that I cannot compile libraries with Visual Studio

and that's wrong, you can compile the libraries with VS, but you cant't link them against your program. Perhaps that was already clear, then excuse my answer.