views:

3043

answers:

5

It is not documented on the web site and people seems having problem setting up the framework. Can someone please show step by step introduction to a sample project setup.

+1  A: 

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();
}
Arlaharen
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.
knaser
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?
Arlaharen
+5  A: 

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.

Nik Reiman
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 http://tinyurl.com/dj5k7k
knaser
+1  A: 

I got a lot of linker errors in Release. In Debug I had no problem. Where is the problem?

A: 

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).

rein
A: 

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

Armando Fonseca