<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Google-Test on despatches</title><link>https://icle.es/tags/google-test/</link><description>Recent content in Google-Test on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Fri, 20 Jun 2025 08:42:17 +0100</lastBuildDate><atom:link href="https://icle.es/tags/google-test/index.xml" rel="self" type="application/rss+xml"/><item><title>Visual Studio, C++ &amp; Google Test</title><link>https://icle.es/2021/08/09/visual-studio-c-google-test/</link><pubDate>Mon, 09 Aug 2021 16:22:08 +0000</pubDate><guid>https://icle.es/2021/08/09/visual-studio-c-google-test/</guid><description>&lt;p>Using Visual Studio for C++ and Google Test seems like it should be absolutely
straightforward. I'm trying to do everything in Visual Studio, so it should
just set it up automatically right at the start when I create a project. Perhaps
I am spoilt from working primarily with Java for many years, but here I was,
trying to set it up and it took an inordinate amount of time.&lt;/p>
&lt;p>First things first, the way it works in C++, at least in Visual Studio, unlike
in Java is that the tests are set up in a separate project, but within the same
solution. I will try and remember all the steps I had to undertake to set this
up.&lt;/p>
&lt;p>This, however, is not the only requirement. When testing, since we can have only
one main function in the final executable, it also makes sense to put all of
your code, apart from the main function for your application in another project.&lt;/p></description><content:encoded><![CDATA[<p>Using Visual Studio for C++ and Google Test seems like it should be absolutely
straightforward. I'm trying to do everything in Visual Studio, so it should
just set it up automatically right at the start when I create a project. Perhaps
I am spoilt from working primarily with Java for many years, but here I was,
trying to set it up and it took an inordinate amount of time.</p>
<p>First things first, the way it works in C++, at least in Visual Studio, unlike
in Java is that the tests are set up in a separate project, but within the same
solution. I will try and remember all the steps I had to undertake to set this
up.</p>
<p>This, however, is not the only requirement. When testing, since we can have only
one main function in the final executable, it also makes sense to put all of
your code, apart from the main function for your application in another project.</p>
<p>In summary, you end up with three projects</p>
<ul>
<li>Your Solution
<ul>
<li>Static Library with all your code *except* the main function</li>
<li>Executable project with your main function (linked with your static library
above)</li>
<li>Tests project (again, linked with your static library above.</li>
</ul>
</li>
</ul>
<p>Once you have this set up, you will want to add a reference to the static
library from both the executable project and the tests project</p>
<p>
  <figure>
    <img src="/assets/2021/08/image.png" alt="Add Reference menu" class="figcaption-img">
    <figcaption>Add Reference</figcaption>
  </figure>

</p>
<p>Finally, you will also have to update the linker to link with the library
project. I am sure there is a better way of doing this, but I did it by right
clicking the <code>project -&gt; Properties -&gt; Linker -&gt; General</code>:</p>
<p><code>Additional Library Directories -&gt;</code> and adding in something like
&ldquo;<code>$(SolutionDir)&lt;lib-folder&gt;\$(IntermediateOutputPath)*.obj</code>&rdquo;</p>
<p>You may have to add the same into <code>Linker -&gt; Input -&gt; Additional Dependencies</code>.</p>
<p>Hope that helps</p>]]></content:encoded></item></channel></rss>