gmock and gtest linker errors in vc++ 2015 community edition - visual-c++

I'm tyring to configure gmock/gtest in vc++ 2015, namely
downloaded gmock and gtest
added E:\googlemock\googletest\include and E:\googlemock\googlemock\include in VC++ include directories.
compiled gmock.sln and added E:\googlemock\googlemock\msvc\2015\Debug to the Library directories.
added gmock.lib to Linker -> Input Additional dependencies.
And on building I'm getting a bunch of linker errors as below.
As i don't have any clue about the gmock/ gtest code. How shall I reason / proceed further in fixing these problems ?
code:
int main(int argc, char **argv)
{
testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
Error **LNK2038** mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in ArrayOperations.obj ConsoleApplication3 E:\projects\cpp\ConsoleApplication3\ConsoleApplication3\gmock.lib(gtest-all.obj)
Error **LNK2005** "public: bool __thiscall std::ios_base::good(void)const " (?good#ios_base#std##QBE_NXZ) already defined in gmock.lib(gtest-all.obj) ConsoleApplication3 E:\projects\cpp\ConsoleApplication3\ConsoleApplication3\msvcprtd.lib(MSVCP140D.dll) 1
and more bunch of errors on the same LNK category*

Finally I could solve the problem by adding in properties -> c++ code generation
Run time Library to Multi-threaded Debug (/MTd) as from the post.
Mismatch Detected for 'RuntimeLibrary'

Related

Latest Visual Studio 2019 v 16.10.0 and _DEPRECATE_TR1_NAMESPACE

I am programming with /std:c++latest in VS2019 v16.10.0. Everything compiled fine with previous version of VS2019. Now I get many errors in header related to _DEPRECATE_TR1_NAMESPACE.
How can I compile around this deprecation?
An example of an error follows:
error C2146: syntax error: missing ';' before identifier 'subtract_with_carry_01'
where the code referenced is:
template <class _Ty, size_t _Wx, size_t _Sx, size_t _Rx>
class _DEPRECATE_TR1_NAMESPACE subtract_with_carry_01
: public _Swc_base<_Ty, _Sx, _Rx, _Swc_01_traits<_Ty, _Wx, _Rx>> { // subtract_with_carry_01 generator
The workaround I found was to
#define _HAS_TR1_NAMESPACE 0
before all header files.
Or to add _HAS_TR1_NAMESPACE=0; in the preprocessor definitions...
This allows me to use latest C++ language!!

<iostream> and #define __STDC_WANT_SECURE_LIB__ 0 results in error C2039: 'sprintf_s'

When building this very simple test program
#include <iostream>
int main() {
std::cout << "x";
}
with visual studio 2019 and /Wall I'm getting a
warning C4668: '__STDC_WANT_SECURE_LIB__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
Trying to
#define __STDC_WANT_SECURE_LIB__ 0
before including iostream results in
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xlocnum(1388,69): error C2039: 'sprintf_s': is not a member of '`global namespace''
at least for my VS. Godbolt doesn't complain.
#define __STDC_WANT_SECURE_LIB__ 1
is fine and doesn't let the compiler complain about sprintf_s which one would expect.
Microsoft doesn't show me any results when searching for it.. SO does here but overall i can't find many resources on if and how to use that define.
Is there a way to disable the secure extensions and include <iostream> ? Am i using the wrong define or approach for this?

Why is the compiler decorating an extern "C" function as a C++ function?

The linker is giving me the following error:
Error 27 error LNK2028: unresolved token (0A0003C0) "extern "C" int
__cdecl InitDSPs(void)" (?InitDSPs##$$J0YAHXZ) referenced in function "public: static unsigned int __clrcall
HikInterface::HikController::InitializeDSPs(enum
HikInterface::VideoStandard)"
(?InitializeDSPs#HikController#HikInterface##$$FSMIW4VideoStandard#2##Z)
As you can see, the linker seems to be expecting a C++ style decorated function, ignoring the extern "C" linkage specification. This only happens on a 64-bit build; The 32-bit build compiles perfectly fine.
I already inspected the exported symbols of the 64-bit .lib file I intend to link and they are not decorated as expected, just like the 32-bit .lib file. I am also sure the project is linking against the correct .lib file.
Here is how the offending function is declared on the header file
#define DLLEXPORT_API extern "C" __declspec(dllexport)
...
DLLEXPORT_API int __stdcall InitDSPs();
(I already tried switching dllexport for dllimport, as this project is consuming the DLL, but the error stays the same)
Any clues or suggestions?

Visual C++ 2013 lnk2019 error

I am just getting started with visual cpp 2013 . I looked around the web for tutorials and tried running the following code.
#include <windows.h>
#include <tchar.h>
int WINAPI winMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
{
MessageBox(NULL, _T("Hello World"), _T("A Sample Application"), MB_ICONINFORMATION);
return 0;
}
It throws the following error
Error 1 error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup C:\Users......\MSVCRTD.lib(crtexew.obj) SampleApp
Can anyone tell me what am i doing wrong here and can someone suggest site for latest tutorials (besides msdn).
I think Raymond Chen already answered your question, but you don't think so. Let me try it again.
From your code, you misspelled 'wWinMain'. The correct one has 8 characters, yours have only 7 characters. You see, that is the problem.
I tried it in VS 2013: replacing your winMain with wWinMain. Project built and ran without problem. Please, try it again.

can we have #include a*.cpp into another *.cpp file in a single project in VC++ 6.0

i am doing some example programs in VC++ 6.0 . For some simple and small programs i dont want to create separate project for each of the program . i have 2 files created in a single project and there is no .h file , so i have included .cpp file into another .cpp file .
If i compile its working but if i build the code its giving error . Following is the code :
file1.cpp :
-----------
#include <iostream>
#include "Calculate_Int.cpp"
using namespace std;
int main ()
{
cout << "\n\nFirst file \n" ;
int x= cal_integer();
return 0;
}
Calculate_Int.cpp:
------------------
#include<iostream>
using std::cout;
using std::endl;
int cal_integer(){
cout<< 1+2<<endl;
cout<<1-5<<endl;
cout<<1-2<<endl;
return 0;
}
If i build this Project1.exe following is the error :
Linking...
Calculate_Int.obj : error LNK2005: "int __cdecl cal_integer(void)" (?cal_integer##YAHXZ) already defined in file_1.obj
Debug/Project_1.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
Project_1.exe - 2 error(s), 0 warning(s)
Please let me know what is wrong .
Including a file is the same as copying the contents of that file in the point you are including it. This means that the contents of Calculate_Int.cpp are compiled twice in your project. And this is what the linker error is telling you: it does not know which version to choose.
In order to compile main.cpp you just need to know the signature of the functions you are using there; this is, you just need a declaration, not a definition. So you can replace this line in main.cpp:
#include "Calculate_Int.cpp"
with this:
int cal_integer();
And your project will compile and link just fine. Anyway, the right way would be to create a file Calculate_Int.h with that line and include it anywhere you need it.

Resources