Can't get google test to work - c++

#include "gtest/gtest.h"
TEST(BattleUnitTest, CountryReturnsProperName) {
EXPECT_EQ(1, 1);
}
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
I can't seem to get google test to work. I ran Nuget package manager to get gtest. It keeps giving me these errors:
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "bool __cdecl testing::internal::IsTrue(bool)" (?IsTrue#internal#testing##YA_N_N#Z) referenced in function "public: void __thiscall testing::internal::scoped_ptr<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::reset(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (?reset#?$scoped_ptr#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###internal#testing##QAEXPAV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) Assignment1_Battle E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj 1
.
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "public: __thiscall testing::Message::Message(void)" (??0Message#testing##QAE#XZ) referenced in function "private: virtual void __thiscall BattleUnitTest_CountryReturnsProperName_Test::TestBody(void)" (?TestBody#BattleUnitTest_CountryReturnsProperName_Test##EAEXXZ) Assignment1_Battle E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj 1
.
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "class testing::AssertionResult __cdecl testing::internal::EqFailure(char const *,char const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?EqFailure#internal#testing##YA?AVAssertionResult#2#PBD0ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##1_N#Z) referenced in function "class testing::AssertionResult __cdecl testing::internal::CmpHelperEQ<int,int>(char const *,char const *,int const &,int const &)" (??$CmpHelperEQ#HH#internal#testing##YA?AVAssertionResult#1#PBD0ABH1#Z) Assignment1_Battle E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj 1
I could post all the errors but almost all of them have to do with LNK2019. Does anyone know how to fix these errors?

I had this problem but there was nothing wrong with my linker settings. I eventually tracked it down to this post PrintTo link issue:
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.
When I changed my setting "Treat WChar_t As Built in Type" to "Yes /Zc:wchar_t" the build worked.

Looks like you need some additional configuration of your test project:
Add gtest library name to you project properties (Project Properties->Configuration Properties->Linker->Input). Be aware that you probably need to add this thing for both Debug and Release configurations and that library names are different (they are gtest.lib for Release and gtestd.lib for Debug; the difference is "d" at the end of the name before dot).
Add the path to the gtest library to your project properties (Project Properties->Configuration Properties->VC++ Directories->Library Directories). This also needs to be done separately for Debug and Release configurations, because the directories are different as well.
If something else goes wrong, this article might be really useful. It contains the full scenario of setting up gtest for a project in Visual Studio without using Nuget. Just be aware that Nuget basically sets up already compiled version of gtest to "package" sub-directory of your project/solution.

Related

Crypto++ library link error using Visual Studio 2017

I'm trying to use the Crypto++ librairy in my project (windows application). Using it, include, compilation work fine, but impossible to deal with the link error
Here is some exemple of link errors, there is more, but don't think it's revelant to copy paste all of them
error LNK2019: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl CryptoV2::encrypt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?encrypt#CryptoV2##SA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V23##Z) referenced in function "public: void __thiscall PStore::storeReversibleCrypt(wchar_t *,char *)" (?storeReversibleCrypt#PStore##QAEXPA_WPAD#Z)
error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl CryptoV2::hashPassword(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?hashPassword#CryptoV2##SA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V23#0#Z)
fatal error LNK1120: 4 unresolved externals
Basically, I add the "Win32\Output\Release" directory of crypto++ to my linker's additionnal library directories properties, and also the main folder to my C/C++'s General's property "Additional Include Directories"
I've tried a lot of thinks, like adding the library as a new project (same errors), adding all cpp files to my project and compiling with it (not compiling), adding only .cpp files I was using (not realistic, too much), linking all different folder of the cryptopp610 releases (cryptdll, cryptlib, dll_output, Output, same errors), and now, I don't really know what more I can try. I makre also a lots of search, trying all solution I saw (don't remember all of them), still the same problems. I also try to create a new project to add crypto++ without long compilation or mysterious problem, but I also get linker error.
Do anyone got any advice to help me ? Anyway, thank's a lot, and pardon my English
EDIT: Need to add that on the new blank project, I get 63 unresolved external symbol, so I think I forgot to do some basic stuff, but can't figure out which
error LNK2019: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl CryptoV2::encrypt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?encrypt#CryptoV2##SA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V23##Z) referenced in function "public: void __thiscall PStore::storeReversibleCrypt(wchar_t *,char *)" (?storeReversibleCrypt#PStore##QAEXPA_WPAD#Z)
error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl CryptoV2::hashPassword(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?hashPassword#CryptoV2##SA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V23#0#Z)
The missing symbols are not from Crypto++. Crypto++ uses the CryptoPP namespace. The missing symbols are from the CryptoV2 namespace or class. I'm guessing that's another crypto library.
You were right in adding directories and a library to the linker settings. However, you need to do it for the CryptoV2 library (in addition to the Crypto++ library).
For completeness, it looks like these are missing:
std::string CryptoV2::encrypt(std::string);
std::string CryptoV2::hashPassword(std::string, std::string);
It could be Ws2_32.lib there is missing from the linker
add #include "dll.h" like stated in the Readme:
To use the Crypto++ DLL in your application, #include "dll.h" before including any other Crypto++ header files, and place the DLL in the same directory as your .exe file. dll.h includes the line #pragma comment(lib, "cryptopp") so you don't have to explicitly list the import library in your project settings.
https://github.com/weidai11/cryptopp/blob/master/Readme.txt#L136

Error building c++ project with ppconsul(Consul client for c++)

I'm a beginner to cpp and trying to use a cpp library called ppconsul(https://github.com/oliora/ppconsul ). This library has dependency to some of other libraries like (boost and curl). I have build ppconsul with all its dependencies and trying to use ppconsul executable in a new project. When I try to build the new project, I'm getting some linker issue like below.
error LNK2001: unresolved external symbol "public: virtual char const * thiscall ppconsul::BadStatus::what(void)const " (?what#BadStatus#ppconsul##UBEPBDXZ)
1>ConsulApp.obj : error LNK2019: unresolved external symbol "class std::basic_string,class std::allocator > __cdecl ppconsul::helpers::encodeUrl(class std::basic_string,class std::allocator > const &)" (?encodeUrl#helpers#ppconsul##YA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##ABV34##Z) referenced in function "void __cdecl ppconsul::parameters::detail::printParameter(class std::basic_ostream > &,class std::basic_string,class std::allocator > const &,struct ppconsul::kw::dc_keyword)" (??$printParameter#Udc_keyword__#kw#ppconsul###detail#parameters#ppconsul##YAXAAV?$basic_ostream#DU?$char_traits#D#std###std##ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##4#Udc_keyword__#kw#2##Z)
I tried to link the project and change the visual studio but nothing worked. Anybody has any idea about this linker issue?
TIA,
Prassi
Issue resolved- Refer this thread github.com/oliora/ppconsul/issues/24
build ppconsul as a static library by passing -DBUILD_STATIC_LIB=ON to CMake.

C++ IDE migration from VS 6 to VS 2013

A C++ project can be compiled by VS6 without errors&warnings.
With VS 2013 Prof., linker problems are occuring. They are providing a pattern namely std::basic_string.
The code was written in 2007 but not by me.
Referenced by a constructor:
vrun.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) public: char const * __thiscall
std::basic_string,class
std::allocator >::c_str(void)const " (__imp_?c_str#?$basic_s
tring#DU?$char_traits#D#std##V?$allocator#D#2##std##QBEPBDXZ)
referenced in fun ction "public: __thiscall VTrs::VTrs(class
VMessageContext &)" (?? 0VDMTrs##QAE#AAVVDMMessageHandlerContext###Z)
In vrun.cpp:
VTrs::VTrs(VMessageContext& handler_):
Inherited(RXS::rName().c_str()),
_handler(handler_)
{
}
(c_str -> const char* c_str() const; --- C98 style --- C11 was not applied in the project till yet)
In vrun.h:
VTrs(VMessageContext&);
Referenced by an operator:
vrun.obj : error LNK2019: unresolved external symbol "__declspec(dllimport)
public: int __thiscall std::basic_string<char,struct std::char_traits<char>,cl
ass std::allocator<char> >::compare(char const *)const " (__imp_?compare#?$basi
c_string#DU?$char_traits#D#std##V?$allocator#D#2##std##QBEHPBD#Z) referenced in
function "bool __cdecl std::operator==<char,struct std::char_traits<char>,clas
s std::allocator<char> >(class std::basic_string<char,struct std::char_traits<c
har>,class std::allocator<char> > const &,char const *)" (??$?8DU?$char_traits#
D#std##V?$allocator#D#1##std##YA_NABV?$basic_string#DU?$char_traits#D#std##V?$a
llocator#D#2##0#PBD#Z)
Refers -as far as interpreted- to a standard operator. (No operator overloading done for == which refers to a string)
By the way, the code itself does not contain "std::basic_string".
The according msdn article was read but this issue was not resolved till yet.
'Usual' issues such as the "C" modifier modifications are already considered for another code.
Any hint how to cope with that?
EDIT: When opening the project the first time with Visual Studio 2013, a kind of conversion dialog was displayed. I agreed but it affected the VC++ Directories. It was visible when comparing the project file with ClearCase to the previous verison. UndoCheckout (via ClearCase) applied the old state. Now it works.
Your linker needs to get the newer MSVC std library from the relevant lib file and it should pick this up when you migrate the project. Take a look at Project Properties->Configuration Properties->Linker->Input and make sure "Ignore All Default Libraries" is set to No
When opening the project the first time with Visual Studio 2013, a kind of conversion dialog was displayed. I agreed but it affected the VC++ Directories. It was visible when comparing the project file with ClearCase to the previous version. UndoCheckout (via ClearCase) applied the old state. Now it works.

I can't get my own static library to work in my project

I've made a static library project and I added it to the solution that has a project which uses the library.
I included the class I needed from it in the main project like I would with another static library but it throws these errors:
error LNK2019: unresolved external symbol "public: __thiscall NetworkingLib::Base::Base(void)" (??0Base#NetworkingLib##QAE#XZ) referenced in function _SDL_main
error LNK2019: unresolved external symbol "public: void __thiscall NetworkingLib::Base::Connect(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?Connect#Base#NetworkingLib##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##0#Z) referenced in function _SDL_main
I have no idea what is going wrong. I checked with another project that uses a library in the same way and I can't find any differences. Any help detecting the problem?
The class NetworkingLib::Base has a method I want to use. So what I did in the main project is:
#include "../NetworkLibrary/Base.h"
NetworkingLib::Base *m_pNetworkObj;
m_pNetworkObj = new NetworkingLib::Base();
m_pNetworkObj->Connect("localhost", "80");
I don't think there's anything wrong with this so the problem must be elsewhere.
It's not enough to have the static library in the same solution. You have to pass it to the linker of the project that uses it.
In Visual Studio, the best way to do this is by adding a "Project Reference". This sets up the dependency so that the projects build in the correct order, and also picks the version of the static library matching the consuming project, be that Debug vs Release, x86 vs x64, etc.

unresolved external symbol while using wchar_t in managed c++ project

I have one C++ project with settings of Unicode as character set and /clr option for common language run time support. I am calling some function of MFC dll (with setting of MultiByte character set) and I am getting some liking error on those functions which are using wchar_t. Same dll is working file for those unicode c++ projects where /clr option is not set (i.e. no common language run time support). I have done the google and tried some thing like:--
Replace Cstring with std:string but both are giving same error.
Example for std: string:--
error LNK2001: unresolved external symbol "public: __thiscall
CEventLog::CEventLog(wchar_t const *)" (??0CEventLog##QAE#PB_W#Z)
for Cstring:--
error LNK2019: unresolved external symbol "public: bool __thiscall
CIITAdoField::GetValue(class ATL::CStringT > > &)"
(?GetValue#CIITAdoField##QAE_NAAV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z)
referenced in function "public: bool __thiscall
Iit::DataFeedSvr::SpeedCache::CacheLoader > >,class
Iit::DataFeedSvr::SpeedCache::HolidayScheduleInfo,struct
Iit::DataFeedSvr::SpeedCache::HolidayScheduleSpeedCacheParam>
::Reload(class Iit::DataFeedSvr::SpeedCache::DoubleKeyCacheInstance > >,class
Iit::DataFeedSvr::SpeedCache::HolidayScheduleInfo,struct
Iit::DataFeedSvr::SpeedCache::HolidayScheduleSpeedCacheParam> &,class
CIITAdoRecordset &,enum
Iit::DataFeedSvr::SpeedCache::ERefreshType,class
Iit::DataFeedSvr::SpeedCache::CacheObserver const *)"
(?Reload#?$CacheLoader#V?$DoubleKeyCacheInstance#JV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL##VHolidayScheduleInfo#SpeedCache#DataFeedSvr#Iit##UHolidayScheduleSpeedCacheParam#456##SpeedCache#DataFeedSvr#Iit###SpeedCache#DataFeedSvr#Iit##QAE_NAAV?$DoubleKeyCacheInstance#JV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL##VHolidayScheduleInfo#SpeedCache#DataFeedSvr#Iit##UHolidayScheduleSpeedCacheParam#456##234#AAVCIITAdoRecordset##W4ERefreshType#234#PBVCacheObserver#234##Z)
I have checked the setting like "Treat Wchar_t as Built in type" and it is file.
I have tried to explicitly export function and class using __declspec(dllexport) but no luck.
I can not change my project from unicode to multibyte or can not change the setting of /clr option as it starts giving other error.
Please suggest the solution. Thanks in advance.
Do a 'dumpbin' on the external DLL to see what's exported. If your CEventLog::CEventLog is not exporting a char based constructor you won't be able to import it, simple as that.
It doesn't look from your example that the /clr switch is causing the problem. Try creating a brand new Unicode project (without the CLR) just to check you really can link to the external DLL.