How to set up Google C++ Testing Framework (gtest) with Visual Studio 2005
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.
Running gtest using Visual Studio 2010: LNK4098 and LNK2005
You have to ensure googletest and your project are built using the same version of the C Runtime Library (CRT). Google test (currently v1.6.0) provides 2 Visual Studio solution files; gtest-1.6.0\msvc\gtest.sln
which uses the static version and gtest-1.6.0\msvc\gtest-md.sln
which uses the dynamic (dll) version. By default, projects created via Visual Studio use the dll version.
You need to decide whether you want your project to use the static or dynamic versions of the CRT.
To set your project to use the static versions, go to Project->Properties
and at the top left of the window, select Configuration: Debug
. Then in this same window select Configuration Properties
-> C/C++
-> Code Generation
. The option for Runtime Library
should be Multi-threaded Debug (/MTd)
.
You then need to ensure you're linking to the appropriate versions of gtest, so select Configuration Properties
-> Linker
-> Input
. Edit the Additional Dependencies
field by providing the full path to the Debug version of the gtest library (e.g. C:\gtest-1.6.0\msvc\gtest\Debug\gtestd.lib
).
Do the same again for Release Configuration, but setting the Runtime Library
option to Multi-threaded (/MT)
and providing the full path to the Release version of the gtest library (e.g. C:\gtest-1.6.0\msvc\gtest\Release\gtest.lib
).
If you decide you want to use the dll versions of the CRT, choose Multi-threaded Debug DLL (/MDd)
and Multi-threaded DLL (/MD)
, and link to the gtest-md libraries which will be in gtest-1.6.0\msvc\gtest-md\...
rather than gtest-1.6.0\msvc\gtest\...
.
Is there any way to make Google testing framework work with Visual Studio 6?
Some time ago, someone proposed a patch for Google Test to support VC6. That patch was ultimately rejected in the trunk because it made it harder to support the library. But the patched source was put in a branch in Subversion for people who need it. There it sits, unsupported.
But because it's not supported, there has been no backports from the trunk since it was branched at version 1.3. But if you really need it and don't mind missing all the features added since then, you can grab it from there.
How to set up unit testing for Visual Studio C++
This page may help, it reviews quite a few C++ unit test frameworks:
- CppUnit
- Boost.Test
- CppUnitLite
- NanoCppUnit
- Unit++
- CxxTest
Check out CPPUnitLite or CPPUnitLite2.
CPPUnitLite was created by Michael Feathers, who originally ported Java's JUnit to C++ as CPPUnit (CPPUnit tries mimic the development model of JUnit - but C++ lacks Java's features [e.g. reflection] to make it easy to use).
CPPUnitLite attempts to make a true C++-style testing framework, not a Java one ported to C++. (I'm paraphrasing from Feather's Working Effectively with Legacy Code book). CPPUnitLite2 seems to be another rewrite, with more features and bug fixes.
I also just stumbled across UnitTest++ which includes stuff from CPPUnitLite2 and some other framework.
Microsoft has released WinUnit.
Also checkout Catch or Doctest
Building/Running Google Test
You appear to have successfully built gtest's libs in Debug mode. However, by default the .sln doesn't contain any executables, so you can't actually run anything.
If you select the checkboxes in the CMake GUI for gtest_build_samples
and/or gtest_build_tests
and hit "Generate", then the Visual Studio solution should contain some executables.
From memory, these don't build cleanly, but I don't have MSVC 2010 to hand, so I can't be sure.
Related Topics
Why Do We Not Have a Virtual Constructor in C++
Metaprograming: Failure of Function Definition Defines a Separate Function
How to Read and Manipulate CSV File Data in C++
How to Get Double Quotes into a String Literal
Can Any One Provide Me a Sample of Singleton in C++
Does Mingw-W64 Support Std::Thread Out of the Box When Using the Win32 Threading Model
Error Lnk2005, Already Defined
Generate Random Double Numbers in C++
Using Scanf() in C++ Programs Is Faster Than Using Cin
How Does the Ampersand(&) Sign Work in C++
Efficient Implementation of Log2(_M256D) in Avx2
Conversion Constructor Vs. Conversion Operator: Precedence
How to Pass a Reference to a Two-Dimensional Array to a Function
Why Should Exceptions Be Used Conservatively
Convert a Vector≪Int≫ to a String
Static_Assert Dependent on Non-Type Template Parameter (Different Behavior on Gcc and Clang)