How to set up Google C++ Testing Framework (gtest) with Visual Studio 2005
Asked Answered
F

6

84

It is not documented on the web site and people seem to be having problems setting up the framework. Can someone please show a step-by-step introduction for a sample project setup?

Fasces answered 10/2, 2009 at 11:32 Comment(1)
Glad someone asked...Boucher
N
45

What Arlaharen said was basically right, except he left out the part which explains your linker errors. First of all, you need to build your application without the CRT as a runtime library. You should always do this anyways, as it really simplifies distribution of your application. If you don't do this, then all of your users need the Visual C++ Runtime Library installed, and those who do not will complain about mysterious DLL's missing on their system... for the extra few hundred kilobytes that it costs to link in the CRT statically, you save yourself a lot of headache later in support (trust me on this one -- I've learned it the hard way!).

Anyways, to do this, you go to the target's properties -> C/C++ -> Code Generation -> Runtime Library, and it needs to be set as "Multi-Threaded" for your Release build and "Multi-Threaded Debug" for your Debug build.

Since the gtest library is built in the same way, you need to make sure you are linking against the correct version of it, or else the linker will pull in another copy of the runtime library, which is the error you saw (btw, this shouldn't make a difference if you are using MFC or not). You need to build gtest as both a Debug and Release mode and keep both copies. You then link against gtest.lib/gtest_main.lib in your Release build and gtestd.lib/gtest_maind.lib in your Debug build.

Also, you need to make sure that your application points to the directory where the gtest header files are stored (in properties -> C/C++ -> General -> Additional Include Directories), but if you got to the linker error, I assume that you already managed to get this part correct, or else you'd have a lot more compiler errors to deal with first.

Nichollenicholls answered 10/2, 2009 at 17:37 Comment(3)
I have learned this the "hard way", spending all my day. Finally I got it working, after building both of them in the same way. Thank you for your answer, but it's late. :/ And BTW, your CRT suggestion is wrong, but I don't have enough space to discuss this. See tinyurl.com/dj5k7kFasces
well if you learnt it the "hard way" you may be forgot that the CRT runtime dlls are called "redistributable" ...so you its clear you have to redistribute them yourself... either by including the redist installer in your own installer, or by simply extracting the needed dlls in your own installation folder... (including a manifest file if needed)Calabro
Statically linking the CRT means your application won't pick up exploit/security fixesAnglocatholic
D
109

(These instructions get the testing framework working for the Debug configuration. It should be pretty trivial to apply the same process to the Release configuration.)

Get Google C++ Testing Framework

  1. Download the latest gtest framework
  2. Unzip to C:\gtest

Build the Framework Libraries

  1. Open C:\gtest\msvc\gtest.sln in Visual Studio
  2. Set Configuration to "Debug"
  3. Build Solution

Create and Configure Your Test Project

  1. Create a new solution and choose the template Visual C++ > Win32 > Win32 Console Application
  2. Right click the newly created project and choose Properties
  3. Change Configuration to Debug.
  4. Configuration Properties > C/C++ > General > Additional Include Directories: Add C:\gtest\include
  5. Configuration Properties > C/C++ > Code Generation > Runtime Library: If your code links to a runtime DLL, choose Multi-threaded Debug DLL (/MDd). If not, choose Multi-threaded Debug (/MTd).
  6. Configuration Properties > Linker > General > Additional Library Directories: Add C:\gtest\msvc\gtest\Debug or C:\gtest\msvc\gtest-md\Debug, depending on the location of gtestd.lib
  7. Configuration Properties > Linker > Input > Additional Dependencies: Add gtestd.lib

Verifying Everything Works

  1. Open the cpp in your Test Project containing the main() function.
  2. Paste the following code:

    #include "stdafx.h"  
    #include <iostream>
    
    #include "gtest/gtest.h"
    
    TEST(sample_test_case, sample_test)
    {
        EXPECT_EQ(1, 1);
    }
    
    int main(int argc, char** argv) 
    { 
        testing::InitGoogleTest(&argc, argv); 
        RUN_ALL_TESTS(); 
        std::getchar(); // keep console window open until Return keystroke
    }
    
  3. Debug > Start Debugging

If everything worked, you should see the console window appear and show you the unit test results.

Durance answered 19/11, 2010 at 4:48 Comment(4)
that was a great guide! I was able to get the ball rolling, but I would add one more note in there: the test project and the gtest library must be built with the same Code Generation option, either both are /MDd or both are /MTd, otherwise there will be a bunch of linking errors.Citarella
Note: Whoever is trying to carry out this similar implementation with Microsoft Visual Studio .Net 2003, the Google Test Framework libraries automatically defaults the Runtime Library to 'Single-threaded Debug' for Debug mode and 'Single-threaded' for Release Mode and as far as I have tried out, the option for changing it from the Code Generation isn't available. So, please make sure you choose Single-threaded option as your runtime library in your project. Other than that, this simple and short tutorial works perfect!Aegir
This is the first of five tutorials that worked and wasn't promoting a bad practice. I would love it if this covered the important aspect of linking the test project to the original project... 30+ sites later, I haven't found a working answer. I've spent 3 days on it.Arquebus
I have removed value in "Project Properties > Configuration Properties > C/C++ > Runtime Library", then it workedRenell
N
45

What Arlaharen said was basically right, except he left out the part which explains your linker errors. First of all, you need to build your application without the CRT as a runtime library. You should always do this anyways, as it really simplifies distribution of your application. If you don't do this, then all of your users need the Visual C++ Runtime Library installed, and those who do not will complain about mysterious DLL's missing on their system... for the extra few hundred kilobytes that it costs to link in the CRT statically, you save yourself a lot of headache later in support (trust me on this one -- I've learned it the hard way!).

Anyways, to do this, you go to the target's properties -> C/C++ -> Code Generation -> Runtime Library, and it needs to be set as "Multi-Threaded" for your Release build and "Multi-Threaded Debug" for your Debug build.

Since the gtest library is built in the same way, you need to make sure you are linking against the correct version of it, or else the linker will pull in another copy of the runtime library, which is the error you saw (btw, this shouldn't make a difference if you are using MFC or not). You need to build gtest as both a Debug and Release mode and keep both copies. You then link against gtest.lib/gtest_main.lib in your Release build and gtestd.lib/gtest_maind.lib in your Debug build.

Also, you need to make sure that your application points to the directory where the gtest header files are stored (in properties -> C/C++ -> General -> Additional Include Directories), but if you got to the linker error, I assume that you already managed to get this part correct, or else you'd have a lot more compiler errors to deal with first.

Nichollenicholls answered 10/2, 2009 at 17:37 Comment(3)
I have learned this the "hard way", spending all my day. Finally I got it working, after building both of them in the same way. Thank you for your answer, but it's late. :/ And BTW, your CRT suggestion is wrong, but I don't have enough space to discuss this. See tinyurl.com/dj5k7kFasces
well if you learnt it the "hard way" you may be forgot that the CRT runtime dlls are called "redistributable" ...so you its clear you have to redistribute them yourself... either by including the redist installer in your own installer, or by simply extracting the needed dlls in your own installation folder... (including a manifest file if needed)Calabro
Statically linking the CRT means your application won't pick up exploit/security fixesAnglocatholic
L
6

I did a video tutorial about the setup: http://www.youtube.com/watch?v=mzSzwQOmMRs

Linnell answered 25/8, 2010 at 4:2 Comment(0)
C
5

Having built gtest, this is what I have done:

  1. Add \mypath\gtest-1.0.1\Debug (or Release) to Common Properties->Linker->General->Additional Library Directories
  2. Add gtest.lib and gtest_main.lib to Common Properties->Linker->Input->Additional Dependencies

After that I just write my tests using TEST or TEST_F as appropriate and compile them together with my main function:

int main(int argc, char** argv)
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
Centralize answered 10/2, 2009 at 12:36 Comment(3)
I get lots of linker errors: already defined in gtest.lib... eg.: 1>LIBCMT.lib(tidtable.obj) : error LNK2005: __encode_pointer already defined in msvcrt.lib(MSVCR80.dll) Oh, btw, I'm trying to write tests for an MFC code.Fasces
Sorry. I don't think I can help you there. For some reason I do not get those link errors. Have you tried to build just a hello world test with gtest?Centralize
gtest_main.lib contains the default main function, so you probably don't want to include it if you wrote your own main explicitly.Inflict
W
5

If you don t want to write your own main() for tests you can use the main() function defined in gtest_main.lib but then you get linker errors "Entry point must be defined" in VS2012. In your test-project set ProjectProperties->Linker->System->SubSystem to "Console" as this will force VS2012 to look for an entry point called "main()" and will find it in gtest_main.lib (provided you ve linked it in properly).

Woolf answered 25/2, 2013 at 23:51 Comment(0)
C
2

In Microsoft Visual Studio, misconfigured runtime library type causes link errors.

VS 2005(and 2008) uses Multithreaded DLL or Multithreaded Debug DLL as default. But Google Test library uses Mulithreaded or Mulithreaded debug runtime as default.

So, choose appropriate run time library type for google test library. (in Configuration properties -> Code Generation -> Runtime Library).

Cinerator answered 14/7, 2009 at 7:1 Comment(1)
what is appropriate runtime? (MD or other)Mitzi

© 2022 - 2024 — McMap. All rights reserved.