Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:41:59+00:00 2026-05-31T12:41:59+00:00

I just downloaded googletest, generated its makefile with CMake and built it. Now, I

  • 0

I just downloaded googletest, generated its makefile with CMake and built it. Now, I need to use it in my testing project.

With CMake, I have been advised not pointing to gtest libraries directly (using include _directories or link_directories) but use find_package() instead.

The problem is, there is no install target for the gtest makefile generated. I cannot understand how find_package(GTest REQUIRED) could work without some kind of installation. Also, putting the gtest folder as a subfolder in my project is not possible.

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:42:00+00:00Added an answer on May 31, 2026 at 12:42 pm

    This is an unusual case; most projects specify install rules.

    CMake’s ExternalProject_Add module is maybe the best tool for this job. This allows you to download, configure and build gtest from within your project, and then link to the gtest libraries.

    I’ve tested the following CMakeLists.txt on Windows with Visual Studio 10 and 11, and on Ubuntu using GCC 4.8 and Clang 3.2 – it might need adjusted for other platforms/compilers:

    cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)
    project(Test)
    
    # Create main.cpp which uses gtest
    file(WRITE src/main.cpp "#include \"gtest/gtest.h\"\n\n")
    file(APPEND src/main.cpp "TEST(A, B) { SUCCEED(); }\n")
    file(APPEND src/main.cpp "int main(int argc, char **argv) {\n")
    file(APPEND src/main.cpp "  testing::InitGoogleTest(&argc, argv);\n")
    file(APPEND src/main.cpp "  return RUN_ALL_TESTS();\n")
    file(APPEND src/main.cpp "}\n")
    
    # Create patch file for gtest with MSVC 2012
    if(MSVC_VERSION EQUAL 1700)
      file(WRITE gtest.patch "Index: cmake/internal_utils.cmake\n")
      file(APPEND gtest.patch "===================================================================\n")
      file(APPEND gtest.patch "--- cmake/internal_utils.cmake   (revision 660)\n")
      file(APPEND gtest.patch "+++ cmake/internal_utils.cmake   (working copy)\n")
      file(APPEND gtest.patch "@@ -66,6 +66,9 @@\n")
      file(APPEND gtest.patch "       # Resolved overload was found by argument-dependent lookup.\n")
      file(APPEND gtest.patch "       set(cxx_base_flags \"\${cxx_base_flags} -wd4675\")\n")
      file(APPEND gtest.patch "     endif()\n")
      file(APPEND gtest.patch "+    if (MSVC_VERSION EQUAL 1700)\n")
      file(APPEND gtest.patch "+      set(cxx_base_flags \"\${cxx_base_flags} -D_VARIADIC_MAX=10\")\n")
      file(APPEND gtest.patch "+    endif ()\n")
      file(APPEND gtest.patch "     set(cxx_base_flags \"\${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32\")\n")
      file(APPEND gtest.patch "     set(cxx_base_flags \"\${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN\")\n")
      file(APPEND gtest.patch "     set(cxx_exception_flags \"-EHsc -D_HAS_EXCEPTIONS=1\")\n")
    else()
      file(WRITE gtest.patch "")
    endif()
    
    # Enable ExternalProject CMake module
    include(ExternalProject)
    
    # Set the build type if it isn't already
    if(NOT CMAKE_BUILD_TYPE)
      set(CMAKE_BUILD_TYPE Release)
    endif()
    
    # Set default ExternalProject root directory
    set_directory_properties(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/ThirdParty)
    
    # Add gtest
    ExternalProject_Add(
        googletest
        SVN_REPOSITORY http://googletest.googlecode.com/svn/trunk/
        SVN_REVISION -r 660
        TIMEOUT 10
        PATCH_COMMAND svn patch ${CMAKE_SOURCE_DIR}/gtest.patch ${CMAKE_BINARY_DIR}/ThirdParty/src/googletest
        # Force separate output paths for debug and release builds to allow easy
        # identification of correct lib in subsequent TARGET_LINK_LIBRARIES commands
        CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
                   -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=DebugLibs
                   -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
                   -Dgtest_force_shared_crt=ON
        # Disable install step
        INSTALL_COMMAND ""
        # Wrap download, configure and build steps in a script to log output
        LOG_DOWNLOAD ON
        LOG_CONFIGURE ON
        LOG_BUILD ON)
    
    # Specify include dir
    ExternalProject_Get_Property(googletest source_dir)
    include_directories(${source_dir}/include)
    
    # Add compiler flag for MSVC 2012
    if(MSVC_VERSION EQUAL 1700)
      add_definitions(-D_VARIADIC_MAX=10)
    endif()
    
    # Add test executable target
    add_executable(MainTest ${PROJECT_SOURCE_DIR}/src/main.cpp)
    
    # Create dependency of MainTest on googletest
    add_dependencies(MainTest googletest)
    
    # Specify MainTest's link libraries
    ExternalProject_Get_Property(googletest binary_dir)
    if(MSVC)
      set(Suffix ".lib")
    else()
      set(Suffix ".a")
      set(Pthread "-pthread")
    endif()
    target_link_libraries(
        MainTest
        debug ${binary_dir}/DebugLibs/${CMAKE_FIND_LIBRARY_PREFIXES}gtest${Suffix}
        optimized ${binary_dir}/ReleaseLibs/${CMAKE_FIND_LIBRARY_PREFIXES}gtest${Suffix}
        ${Pthread})
    

    If you create this as CMakeLists.txt in an empty directory (say MyTest), then:

    cd MyTest
    mkdir build
    cd build
    cmake ..
    

    This should create a basic main.cpp in MyTest/src and create a project file (MyTest/build/Test.sln on Windows)

    When you build the project, it should download the gtest sources to MyTest/build/ThirdParty/src/googletest, and build them in MyTest/build/ThirdParty/src/googletest-build. You should then be able to run the MainTest target successfully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just downloaded the Silverlight 3 Toolkit and executed the MSI file. Now I need
I just downloaded Notepad++ because I need to work on a classic asp project.
Just downloaded the Visual Studio 2010 Premium and realized that can't compile the project,
I just downloaded the WPF Reports v0.4 alpha When I build the project in
I just downloaded Scrapy (web crawler) on Windows 32 and have just created a
I just downloaded Netty for a personal client-server-project from here: http://netty.io/downloads/netty-3.3.0.Final-dist.tar.bz2 On the download
I just downloaded Grails 2.0.1. Created a new project and then tried adding the
I just downloaded newsest doc list api, and have a try. My code as
Just downloaded libcurl and built it on Windows, using VC2008. It came with a
I just downloaded an Xcode project and all the frameworks are missing. It wouldn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.