PrintTo link issue

820 views
Skip to first unread message

benoit vedrenne

unread,
Oct 28, 2011, 5:54:04 AM10/28/11
to googlemock
Hi,

I don't know if it's the right place to submit but I have an issue that I don't understand.
I create a mock object and a test.
When in the test, I try to instanciate the mock object then I have a link issue :

error LNK2019: unresolved external symbol "void __cdecl testing::internal::PrintTo(
unsigned short,class std::basic_ostream<char,struct std::char_traits<char> > *)" (?PrintTo@internal@testing@@YAXGPAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z) referenced in function "public: static void __cdecl testing::internal::UniversalPrinter<unsigned short>::Print(unsigned short const &,class std::basic_ostream<char,struct std::char_traits<char> > *)" (?Print@?$UniversalPrinter@G@internal@testing@@SAXABGPAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z)

I'm on Windows XP with Visual Studio 2005.
I am using GMock and GTest 1.5 but I have already try with the 1.6.0 version of both without any change.

Can you help me?

Regards

Ben

Greg Miller

unread,
Oct 28, 2011, 2:06:12 PM10/28/11
to benoit vedrenne, googlemock
Hi, Benoit.

Have you added a gmock.vcproj reference to your project, or added gmock.lib explicitly to your linker settings? 
You can verify in your saved build log whether your project links in gmock.lib.

HTH,

Greg

Janne Hakonen

unread,
Nov 24, 2011, 4:03:37 AM11/24/11
to Google C++ Mocking Framework
Hello,

I seem to have this same problem with Visual Studio 2008 SP1. I have
the static libraries of Google Mock 1.6 which I have compiled myself.
I have added both the library and headers to my project. The library
works fine until I try to define a mock method which has unsigned
short argument. Signed short works fine.

Compile output:
1>------ Build started: Project: GMockTest, Configuration: Debug Win32
------
1>Compiling...
1>main.cpp
1>Linking...
1>LINK : C:\Projects\MSVC\GMockTest\Debug\GMockTest.exe not found or
not built by the last incremental link; performing full link
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl
testing::internal::PrintTo(unsigned short,class


std::basic_ostream<char,struct std::char_traits<char> > *)" (?
PrintTo@internal@testing@@YAXGPAV?$basic_ostream@DU?
$char_traits@D@std@@@std@@@Z) referenced in function "public: static
void __cdecl testing::internal::UniversalPrinter<unsigned
short>::Print(unsigned short const &,class
std::basic_ostream<char,struct std::char_traits<char> > *)" (?Print@?
$UniversalPrinter@G@internal@testing@@SAXABGPAV?$basic_ostream@DU?
$char_traits@D@std@@@std@@@Z)

1>C:\Projects\MSVC\GMockTest\Debug\GMockTest.exe : fatal error
LNK1120: 1 unresolved externals
1>Build log was saved at "file://C:\Projects\MSVC\GMockTest\Debug
\BuildLog.htm"
1>GMockTest - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

Here is small example code which causes the error:
#include <gmock/gmock.h>

class MockBug
{
public:
MOCK_METHOD1( foo, void( unsigned short bar ) ); // link error
};

int main(int argc, char *argv[])
{
MockBug bug;
return 0;
}

As a workaround, if I define following, before the class declaration,
the code compiles fine:
namespace testing {
namespace internal {
void PrintTo( unsigned short value, std::ostream* os ) { }
}
}

Best Regards,
Janne Hakonen

On Oct 28, 8:06 pm, Greg Miller <j...@google.com> wrote:
> Hi, Benoit.
>
> Have you added a

> gmock.vcproj<http://code.google.com/p/googlemock/source/browse/#svn%2Ftrunk%2Fmsvc...>reference


> to your project, or added gmock.lib explicitly to your linker
> settings?
> You can verify in your saved build log whether your project links in
> gmock.lib.
>
> HTH,
>
> Greg
>
> On Fri, Oct 28, 2011 at 5:54 AM, benoit vedrenne

> <benoit.vedre...@gmail.com>wrote:

Vlad Losev

unread,
Nov 25, 2011, 10:01:45 PM11/25/11
to Janne Hakonen, Google C++ Mocking Framework
Janne,

I was unable to reproduce the problem. I have installed VC++ 2005 Express and Windows SDK, opened the gmock.sln from the distribution in Visual Studio, added a test project, made it depend on gmock.vcproj, adjusted the include paths and the runtime library settings and put your snippet into the source file. It has compiled and run fine.

Maybe there is something peculiar about your project settings. Do your library and your tests have the same /Zc:wchar_t setting, for example? If you can isolate the problem, please share it.

Regards,
Vlad

Janne Hakonen

unread,
Nov 28, 2011, 3:06:05 AM11/28/11
to Google C++ Mocking Framework
Hi Vlad,

This was indeed caused by the mismatching /Zc:wchar_t setting.
I failed to mention in the previous post that I had made that example
code as Qt Console Application project (with Qt Add-in 1.1.9).
Apparently the wchar_t setting is set to 'no' with that project
template, while Google Mock's project setting is set to 'yes' by
default.

When I recompiled Google Mock with the setting set to 'no' the linker
problem went away.

Thanks for help!
Janne

anca....@gmail.com

unread,
Aug 10, 2016, 10:48:35 AM8/10/16
to Google C++ Mocking Framework
I know this thread is old, but I managed to reproduce the problem with the latest gmock version, VisualStudio 2008 and Qt 4.8.

I have a mock file that compiles and links just fine until I try to mock a method that contains an unsigned short, either as an argument or as a return type.

This (and anything similar) works:
    MOCK_METHOD2(methodOK, bool(QString, QString));


These don't:
    MOCK_METHOD2(openOutgoing, bool(QString, unsigned short));
    MOCK_METHOD2(openOutgoing, unsigned short(QString, bool));
(same, of course, if quint16 is used instead of unsigned short)

Linker error:
error LNK2019: unresolved external symbol "void __cdecl testing::internal::PrintTo(unsigned short,class std::basic_ostream<char,struct std::char_traits<char> > *)" (?PrintTo@internal@testing@@YAXGPAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z) referenced in function "public: static void __cdecl testing::internal::UniversalPrinter<unsigned short>::Print(unsigned short const &,class std::basic_ostream<char,struct std::char_traits<char> > *)" (?Print@?$UniversalPrinter@G@internal@testing@@SAXABGPAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z)

Worked with the workaround mentioned above:

namespace testing {
  namespace internal {
    void PrintTo( unsigned short value, class std::basic_ostream<char,struct std::char_traits<char> > * os ) { }
  }
}

Should I reopen an issue on github or is it something that I'm missing?

anca....@gmail.com

unread,
Aug 10, 2016, 10:48:35 AM8/10/16
to Google C++ Mocking Framework
Ahh, I'm sorry about that, changing the settings for the gmock project did the trick for me too...
Thank you.


luni, 28 noiembrie 2011, 10:06:05 UTC+2, Janne Hakonen a scris:
Reply all
Reply to author
Forward
0 new messages