title: CMake: Adding GTest to Build
body: <p>I'm trying to add googletest to my build using the <code>FetchContent</code> module, but I've come across many partial answers and have been unable to get where I want. There seems to be an excess of dated examples that use the <code>ExternalProject</code> module, but very few helpful examples that use the <code>FetchContent</code> module.</p>
  
  <p>Here's an incomplete list of references I've gone through today:</p>
  
  <ul>
  <li><a href="https://crascit.com/2015/07/25/cmake-gtest/" rel="noreferrer">https://crascit.com/2015/07/25/cmake-gtest/</a></li>
  <li><a href="https://github.com/bast/gtest-demo" rel="noreferrer">https://github.com/bast/gtest-demo</a></li>
  <li><a href="https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project" rel="noreferrer">https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project</a></li>
  <li><a href="https://cmake.org/cmake/help/latest/module/FetchContent.html#declaring-content-details" rel="noreferrer">https://cmake.org/cmake/help/latest/module/FetchContent.html#declaring-content-details</a></li>
  <li><a href="https://stackoverflow.com/questions/8507723/how-to-start-working-with-gtest-and-cmake">How to start working with GTest and CMake</a></li>
  </ul>
  
  <p>Here's the file structure that I want to use:</p>
  
  <pre><code>-root
      -build
          -googletest-build
          -googletest-src
          -googletest-test
          -src
          -test
      -src
          -CMakeLists.txt
          -AllOfMySourceFiles
      -test
          -CMakeLists.txt
          -testgtest.cpp
      -CMakeLists.txt
  </code></pre>
  
  <p>These are the commands that I'm trying to use to build:</p>
  
  <pre><code>cmake ..
  cmake --build .
  </code></pre>
  
  <p>So far, I'm able to link all of my source files into the dll that I want and I'm fairly confident that I know how to link it with the test exe that I want to use. The main issue I'm having is using the <code>FetchContent</code> module to build <code>testgtest.cpp</code> into an executable and link it with googletest. It keeps failing to build and the specific reason why it's failing is not obvious at all.</p>
  
  <p><strong>root/CMakeLists.txt</strong></p>
  
  <pre><code>cmake_minimum_required(VERSION 3.0)
  set(CMAKE_CXX_FLAGS_RELEASE "/MT")
  set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
  set(CMAKE_CXX_STANDARD 11)
  
  set(LIBNAME "WTP")
  set(TESTNAME "TestRunner")
  
  set(LIB_MAJOR_VERS "0")
  set(LIB_MINOR_VERS "0")
  set(LIB_PATCH_VERS "0")
  
  #build source
  project(${LIBNAME} 
      VERSION  ${LIB_MAJOR_VERS}.${LIB_MINOR_VERS}.${LIB_PATCH_VERS})
  add_subdirectory(src)
  
  #build tests
  project(${TESTNAME})
  
  add_subdirectory(test)
  enable_testing()
  add_test(${TESTNAME} ${TESTNAME})
  </code></pre>
  
  <p><strong>src/CMakeLists.txt</strong></p>
  
  <pre><code># Build output setup
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/src/lib)
  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/src/lib)
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/src/bin)
  
  #get all of the source files
  set(SRCS Export.c
      Regions/ReferenceConstants.h
      Regions/Region1.h
      Regions/Region2.h
      Regions/Region3.h
      Regions/Boundaries/RegionBoundaries/R2andR3.h
      Regions/Boundaries/SubregionBoundaries/Region3/Boundaries_vTP.h)
  
  add_library(${LIBNAME} SHARED ${SRCS})
  </code></pre>
  
  <p>This one is working as I intend it to so far.</p>
  
  <p><strong>test/CMakeLists.txt</strong></p>
  
  <pre><code># Build output setup
  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test/lib)
  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test/lib)
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test/bin)
  
  ################################
  # GTest
  ################################
  project(googletest-git NONE)
  
  include(FetchContent)
  FetchContent_Declare(
    googletest
    GIT_REPOSITORY https://github.com/google/googletest.git
    GIT_TAG        release-1.8.0
  )
  
  set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
  set(BUILD_GTEST ON CACHE BOOL "" FORCE)
  
  FetchContent_MakeAvailable(googletest)
  
  ################################
  # Tests
  ################################
  # Add test cpp file
  add_executable(${TESTNAME} testgtest.cpp)
  # Link test executable against gtest &amp; gtest_main
  target_link_libraries(${TESTNAME} gtest gtest_main)
  </code></pre>
  
  <p>I'm unclear on what I should be doing specifically here for A) using the <code>FetchContent</code> module to add googletest to my build <strong>AND</strong> B) linking my test executable with googletest.</p>
  
  <p><strong>test/testgtest.cpp</strong></p>
  
  <pre><code>#include "gtest/gtest.h"
  
  TEST(sample_test_case, sample_test)
  {
      EXPECT_EQ(1, 1);
  }
  </code></pre>
  
  <p>It's not obvious what I should be <code>#include</code>ing specifically in <code>testgtest.cpp</code>, but I saw that in the examples I've seen so I figured that this is some obtuse, arcane magic that's just <em>supposed</em> to work.</p>
  
  <p>Here's an output of what I've been seeing:</p>
  
  <pre><code>$ cmake --build .
  Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Framework
  Copyright (C) Microsoft Corporation. All rights reserved.
  
    gtest-all.cc
  D:\root\build\_deps\googletest-src\googletest\include\gtest/internal/gtest-port.h(996,34): error C2220:  warning treated as error - no 'object' file generated [D:\root\build\_deps\googletest-build\googletest\gtest.vcxproj]
  D:\root\build\_deps\googletest-src\googletest\include\gtest/internal/gtest-port.h(996,34): warning C4996:  'std::tr1': warning STL4002: The non-Standard std::tr1 namespace and TR1-only machinery are deprecated and will be REMOVED. You can define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING to acknowledge that you have received this warning. [D:\root\build\_deps\googletest-build\googletest\gtest.vcxproj]
  </code></pre>
  
  <p>The <code>warning C4996</code> will then continue to just repeat ad nauseam until it finishes.</p>
  
  <p>I do want to fetch googletest from git, so what do I need to be doing differently here to successfully build and use googletest with my tests?</p>
  
  <p>Thanks for reading</p>