Setting up Visual Studio 2013

2,059 views
Skip to first unread message

super baran

unread,
Nov 4, 2013, 6:25:42 PM11/4/13
to googletes...@googlegroups.com
Hi !
I am trying to set up GoogTest with Visual Studio 2013 ( since it has all the new C++11 features that I am planning to use) and ran into problem error:

1>------ Build started: Project: UnitTest, Configuration: Debug Win32 ------
1>Source.obj : error LNK2019: unresolved external symbol "int __cdecl Factorial(int)" (?Factorial@@YAHH@Z) referenced in function "private: virtual void __thiscall FactorialTest_Negative_Test::TestBody(void)" (?TestBody@FactorialTest_Negative_Test@@EAEXXZ)
1>Source.obj : error LNK2019: unresolved external symbol "bool __cdecl IsPrime(int)" (?IsPrime@@YA_NH@Z) referenced in function "private: virtual void __thiscall IsPrimeTest_Negative_Test::TestBody(void)" (?TestBody@IsPrimeTest_Negative_Test@@EAEXXZ)
1>C:\Users\alex\Documents\Visual Studio 2013\Projects\Sample_1\Debug\UnitTest.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Here is what I did so far:

  1. Opened, converted, compiled the \gtest-1.7.0\msvc\gtest.sdf in released and debug mode
  2. Created solution Sample_1 as empty Windows console application
  3. Added two files to it sample1.h & sample1.cpp (both taken from googletest official site http://code.google.com/p/googletest/source/browse/#svn/trunk/samples)
  4. Added New project Sample_1_UnitTest to the Sample_1 solution
  5. In the Sample_1_UnitTest properties page:

Configuration Properties ->
-> VC++ Directories->Include Directories -> C:......\gtest-1.7.0\include & \gtest-1.7.0 (for All Configurations)
-> C/C++ -> General -> Include Directories -> C:......\gtest-1.7.0\include (for All Configurations)
-> C/C++ -> Preprocessor -> Preprocessor Definition -> add _VARIADIC_MAX=10 (for All Configurations)
-> C/C++ -> Code Generation -> Runtime Library - > /MT (for Release) & /MTd (for Debug)
-> Linker -> General -> Additional Lib.D. -> \gtest-1.7.0\msvc\gtest\Release (for Release) & \Debug (for debug)
-> Linker -> Input -> Additional Dep.-> gtest(d).lib; gtest_main(d).lib(Depending on release/debug)


6. Added file sample1_unittest.cpp (http://code.google.com/p/googletest/source/browse/trunk/samples/sample1_unittest.cc) to the Sample_1_UnitTest project.
7. Added Reference to Sample_1_UnitTest project.
8. Build solution (in debug), and getting following error:

I have tried to be as accurate as possible, though there are so many steps, that I might have forgotten/missed something.
Thanks !

Josh Kelley

unread,
Nov 4, 2013, 9:03:22 PM11/4/13
to super baran, Google C++ Testing Framework
If you have two separate application projects, then the Sample_1_UnitTest project has no way of finding the functions that you're trying to test in the Sample_1 application.  You can link the unit tests, Google Test, and code under test into the same executable, or you can build the code under test as a library and link it to your test executable.  (Building the tests and Google Test as a library is also possible, although it can be tricky and is not recommended; see http://code.google.com/p/googletest/wiki/Primer#Important_note_for_Visual_C++_users.)

--
Josh Kelley


--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

alex

unread,
Nov 5, 2013, 2:23:51 PM11/5/13
to Josh Kelley, Google C++ Testing Framework
Hi Josh, thank you for reply.
I spent my entire day yesterday and good portion of the night(went to bed at 5 in the morning), trying to make it work, I finally settled down with this method: http://www.bogotobogo.com/cplusplus/google_unit_test_gtest.php
. This method work only when functions/classes definitions are within .h files(basically same error as the one I described above), separate compilation does not work. I would highly appreciate, if you could describe(or provide a link) me a good method that will work. Thanks ! -Alex

Josh Kelley

unread,
Nov 5, 2013, 9:37:52 PM11/5/13
to alex, Google C++ Testing Framework
That's actually not a very good tutorial.  (Putting a non-inline function in a header file is bad C++ style and is going to cause problems for almost any nontrivial project.)

I've done very little in Visual C++ specifically, but here's the general idea.
  • You have some code that you want to test (Factorial, IsPrime, Cubic, whatever, from sample1.cc).
  • You have an application that uses that code (your Sample_1 console app containing a main() function).
  • You want to write some unit tests to run in their own console app (Sample_1_UnitTest).
The way you did it, the code that you want to test is only linked to Sample_1, so you're getting unresolved symbols within Sample_1_UnitTest.

There are a couple of ways to handle this:
  • You can directly include the source files for the code you want to test in both your application and your unit tests.  (In other words, the Visual C++ projects for both Sample_1 and Sample_1_UnitTest would contain sample1.cc in addition to their own main() function and unit tests, respectively.)
  • You can create a separate project, a static library or DLL containing the code you want to test.  Both your application and your unit tests would link to this library.  This is the best long-term solution.

For background information on static libraries and DLLs in C++, see http://stackoverflow.com/q/1127460/25507.

Josh Kelley

Reply all
Reply to author
Forward
0 new messages