Skip to content

Instantly share code, notes, and snippets.

@tcavallari
Forked from oneamtu/gtest.cmake
Last active October 1, 2015 04:07
Show Gist options
  • Save tcavallari/5aecd60f5daab53e5b38 to your computer and use it in GitHub Desktop.
Save tcavallari/5aecd60f5daab53e5b38 to your computer and use it in GitHub Desktop.
########################### GTEST
# Enable ExternalProject CMake module
INCLUDE(ExternalProject)
# Set default ExternalProject root directory
SET_DIRECTORY_PROPERTIES(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/3rdparty)
# Add gtest
# http://stackoverflow.com/questions/9689183/cmake-googletest
ExternalProject_Add(
googletest
URL https://googletest.googlecode.com/files/gtest-1.7.0.zip
# TIMEOUT 10
# # 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_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=Debug
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=Release
-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)
set(GTEST_INCLUDE_DIR ${source_dir}/include)
# Library
ExternalProject_Get_Property(googletest binary_dir)
set(GTEST_LIBRARIES gtest)
add_library(${GTEST_LIBRARIES} UNKNOWN IMPORTED)
set_property(TARGET ${GTEST_LIBRARIES} PROPERTY
IMPORTED_LOCATION_DEBUG "${binary_dir}/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}${GTEST_LIBRARIES}${CMAKE_FIND_LIBRARY_SUFFIXES}")
set_property(TARGET ${GTEST_LIBRARIES} PROPERTY
IMPORTED_LOCATION_RELEASE "${binary_dir}/Release/${CMAKE_FIND_LIBRARY_PREFIXES}${GTEST_LIBRARIES}${CMAKE_FIND_LIBRARY_SUFFIXES}")
add_dependencies(${GTEST_LIBRARIES} googletest)
set(GTEST_MAIN_LIBRARIES gtest_main)
add_library(${GTEST_MAIN_LIBRARIES} UNKNOWN IMPORTED)
set_property(TARGET ${GTEST_MAIN_LIBRARIES} PROPERTY
IMPORTED_LOCATION_DEBUG "${binary_dir}/Debug/${CMAKE_FIND_LIBRARY_PREFIXES}${GTEST_MAIN_LIBRARIES}${CMAKE_FIND_LIBRARY_SUFFIXES}")
set_property(TARGET ${GTEST_MAIN_LIBRARIES} PROPERTY
IMPORTED_LOCATION_RELEASE "${binary_dir}/Release/${CMAKE_FIND_LIBRARY_PREFIXES}${GTEST_MAIN_LIBRARIES}${CMAKE_FIND_LIBRARY_SUFFIXES}")
add_dependencies(${GTEST_MAIN_LIBRARIES} googletest)
set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
@kylemanna
Copy link

Tried this because it looked really clean but am getting:

Manually-specified variables were not used by the project:
    CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG
    CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE

From the gtest build using cmake 3.3.2. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment