Issue setting up gmock on visual studio 2019 - c++

I'm trying to setup gmock on visual studio 2019.
I have my folder structure as below
Here i have configured my project with source code(Gmock_project) as a static library. And i have made runtime library as Multi-threaded DLL (/MD). I'm also not using any any precompiled headers.
Then for the unit test project i have done the following properties.
1) Created a new project from the Google Test template (Gmock_Sample).
2) Installed the latest gmock NuGet package from Google (currently v1.10.0).
3) Added the file gtest_main.cc to the project.
4) I have disabled the precompiled headers for gtest_main.cc, gmock-all.cc and gtest-all.cc.
5) I have added the reference of the Gmock_project to the test project.
6) I have included the path of the Gmock_project in VC++directories -> Include directories.
7) I have made runtime library as Multi-threaded DLL (/MD).
8) I have added the path of Gmock_project.lib in Linker -> General -> Additional Library Directories.
9) I have included the Gmock_project.lib in Linker -> Input -> Additional Dependencies.
Whenever i'm building Gmock_Sample i.e. test project i'm getting error
2>test.obj : error LNK2005: main already defined in gtest_main.obj
2>E:\GMock\Gmock_Sample\Gmock_Sample\x64\Release\Gmock_Sample.exe : fatal error LNK1169: one or more multiply defined symbols found
2>Done building project "Gmock_Sample.vcxproj" -- FAILED.
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
What could be the issue in configuration?

I fixed this issue today by doing a CMAKE Build for Windows using the googletest repository and then adding the static .lib files and headers into my project.
Here are the steps:
1.Clone googletest Repository -> Git-Repository
2.Download CMAKE for Windows. -> CMAKE For Windows
3.Install CMAKE, Don't forget to create the System Variables to call it from the CMD.
4.Go to the cloned Repository Folder.
5.Create a folder called "build".
6.Create a folder in your solution application called "googletest-package" -> the .lib files and headers will be exported here.
Run the following command in the cmd or powershell.:
cmake -G "Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX=C:\YourSolutionFolder\googletest-package ..
In your build folder you will find now the googletest-distribution.sln -> Open the file with a double click.
In Visual Studio build the ALL_BUILD Project and then the INSTALL Project this will move the headers and .lib files to the googletest-package folder in your solution.
--First Stage Finished!
For the Second Stage we need to do the following since we already have the headers and static libraries its time to link them to the project we want to test.
Create a new c++ project in the solution you are trying to test.
-> Richt Click Solution -> Add -> New Project-> Google Test -> Configuration as Static Library and Link statically.
We need to remove first the googletest NU Package installed automatically from the test project to avoid duplicates and clashes with our own googletest build.
GO TO -> Tools -> NUGet Package Manager -> Manage NuGet Packages for Solution -> Uninstall Microsoft.googletest.vXXX...
Now Add the Include Folders and link the .lib files to the test project as follows:
-> Right Click in the test project -> C/C++ Tab -> Additional Include Folders
Add the following includes...
"YourSolutionFolder\googletest-package\include"
"YourSolutionFolder\googletest-package\include\gmock"
"YourSolutionFolder\googletest-package\include\gtest"
Now Time for the Linker Tab...
-> Linker -> General -> Additional Library Directories -> Add "YourSolutionFolder\googletest-package\lib"
Add the .lib objects as follows:
Linker -> Input -> Additional Dependencies -> Add the following...
"gmockd.lib;gtestd.lib;mock_maind.lib;gtest_maind.lib"
5.In your pch.h file add the following line #include "gmock.h".
You should now be able to build all projects and run tests with mocks!
I hope this helped allthough is a little bit long I think it could be automated.

Related

How to add a library to my project in a visual studio 2019?

I needed to use zydis library in my VC2019 cpp project.
I have no idea how can I add it to my current project - I downloaded it from github.
There is msvc folder, inside I can find .sln file, some header files and .vcxproj files - what should I do, to just include it into my project and use it?
It can be done by adding a reference to the DLL file.
In Visual Studio, right click on the project, Add Reference. Give the path to the DLL file and add it to the project.
First, I suggest that you could download and install Zydis using the vcpkg dependency manager. The method is easy and convenient.
If you don't use vcpkg, you could follow the steps below.
Open the Zydis.sln in msvc with VS2019.
Copy files in include/Zycore, put them in zydis-master\include\Zycore. Because I find that there should be missing files when I compile Zydis.sln.
Right click Zydis, set Visual Studio 2019(v142) in Properties->General->Platform Toolset.
Compile it, and you will find Zydis.lib in zydis-master\msvc\bin\DebugX64.
Then, you could copy include floder and lib in your program floder.
You could set VS:
Right-click the project, select Properties->Configuration
Properties->C/C++->General, find the Additional Include Directories and set the directory.
Properties -> Configuration Properties -> Linker ->
General, find the Additional library directory in General, and set the lib.
Properties -> Configuration Properties -> Linker -> Input,
find Additional Dependencies and input the lib name.
Besides, you could set five build configurations.

How do I use cURLpp with Visual Studio?

I'm using VS 2019, have downloaded all the cURLpp headers, put them and the cURL headers in an include folder and added this include directory under Project Properties -> Additional Include Directories. When I try to build example00.cpp from the cURLpp site, I get errors saying I have an unresolved external symbol. I've never used a third party library with C++ before, so please explain like I'm an idiot. Should I have a dll or lib file?
I use vcpkg to install curlpp, and I find that my project configuration in visual studio is x64, if I use x86 curlpp it will get build error, I need to use x64 curlpp then build sucess.
This is the include step:
Run cmd command: vcpkg install curlpp:x64-windows
goto Project > Configuration Properties -> Linker -> General -> Additional Library Directories, add yourVcpkgFolderPath\vcpkg\installed\x64-windows\lib\
goto Project > Configuration Properties -> Linker -> Input -> Additional Dependencies, add curlpp.lib
Then build the project, it should build success.

Visual Studio static library : NuGet package not found when using in another project

I'm building a static library using Visual Studio, which involves installing nupengl and glm. Then I linked it to a test project via this method :
Project Properties -> VC++ Directories -> Additional include library (I entered the directory which contains my header files from the library)
Project Properties -> Linker -> Input (I entered the directory which contains the .lib file for the library)
I added the header files in the library to my test project
But when I run the test program, it produces things like :
GLFW/glfw3 not found
How can I use the installed NuGet package for the library when calling the library from another project ? Any help would be highly appreciated

How to use SDL locally in Visual Studio 2013

I want to add SDL and SDL_image to my Visual Studio project. But can I do it locally only for this project? I don't want to put the dlls in System32 folder.
As described by Ciprian Khlud, you could simply put your DLLs next to the output binary (.exe). You could find output directory in
Right click on project -> Project Settings -> General -> Output Directory
Alternatively, you could add the folder where DLLs are into the list of search directories:
Right click on project -> Project Settings -> VC++ Directories -> Executable Directories
(same way as you add include dirs and library dirs)
An easy way is to copy them either manually into your output folder or to create a post build event.
Use Microsoft link to see where you can put your Dlls without conflicting with System32:
https://msdn.microsoft.com/en-us/library/7d83bc18.aspx

Visual Studio 2010: dll missing

I googled for a whole day and I'm goin' mad..
Well, that's my problem: I've written my vs project, I've specified all the "include" (by selecting project properties -> configuration properties -> VC++ directories) and all the extern libraries directories (in the same way).
Then I specified all the additional libraries by selecting project properties -> Linker -> input -> Additional Dependencies and adding all the .lib files paths.
I press F7, it compiles with no errors.
I run the project and.. System Error: libsndfile-1.dll is missing.
That's okay, so I opened the vs console, I moved to libsndfile-1.def (and libsndfile-1.lib) path and I executed: "lib.exe libsndfile.def". Here I got another error: "LINK: fatal error lnk1104 lib.exe, impossible to open libsndfile.lib"
I tried to download libsndfile.dll from web but it still don't works. I've also tried to put all the libraries in my source files in visual studio.. but again, no way.
Where am I wrong?
Thank you in advance
EDIT: I've seen that vs doesn't find the other dll also!
Please do not copy the DLL into the folder... this is annoying for other developers in your organisation; instead right-click your app and click properties, go into the Configuration Properties->Debugging and set the Environment value to:
PATH=C:\PathToInclude;%PATH%
this will add the path to your environment and merge it to your application environment.
Do that for all Configurations of your app.
I solved simply placing all the dll files in the same directory of the .exe file