Adding GoogleTest to an existing C++ project
2025年1月9日Unfortunately Stackoverflow is not greenhorn-friendly. Questions asked by newbies are almost always downvoted.
Just follow http://www.myoddweb.com/2019/11/09/add-google-test-directly-to-your-visual-studio-project/
Start with the template of C++ Console App. You will delete the default ConsoleApplication.cpp
because the main
function in this file conflicts with the main function in gtest. Then the project should compile.
Do not fiddle around with the entry point option of a C++ project in Visual Studio. The entry point option is adult-only. Learners shouldn’t touch it. This option does not allow you to switch entry points.
How to switch between GoogleTest project and Console application?
The core difference between GoogleTest project and Console application is the main function. For a regular console app, you use main() in ConsoleApplication.cpp
. For a GoogleTest run, you use use main() in gtest_main.cc
.
As I said, do not set the entry point option to change compilation modes. However, we can use configurations and conditionals in MSBuild project files.
First, create a new configuration in confirguation manager. Copy from an existing one, such as Debug. Here I name it ConsoleDebug.
Then include ConsoleApplication.cpp or gtest files conditionally based on the ConsoleDebug config.
<ItemGroup Condition="'$(Configuration)'=='ConsoleDebug'">
<ClCompile Include="ConsoleApplication.cpp" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'!='ConsoleDebug'">
<ClCompile Include="googletest-1.15.2\googletest\src\gtest-all.cc" />
<ClCompile Include="googletest-1.15.2\googletest\src\gtest_main.cc" />
</ItemGroup>