0

I'm trying to use gtest into an existing cmake project. I'm following this steps:

CMakeLists.txt

project(myproject C CXX)

# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
                 ${CMAKE_BINARY_DIR}/googletest-build
                 EXCLUDE_FROM_ALL)

# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
  include_directories("${gtest_SOURCE_DIR}/include")
endif()

find_library(GTEST_LIB gtest)
message(STATUS ${GTEST_LIB})

CMakeLists.txt.in

cmake_minimum_required(VERSION 2.8.2)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
  GIT_REPOSITORY    https://github.com/google/googletest.git
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_BINARY_DIR}/googletest-src"
  BINARY_DIR        "${CMAKE_BINARY_DIR}/googletest-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

And, I'm getting this message "GTEST_LIB-NOT FOUND". Is there a way to fix this?

I've already tried the solutions mentioned here: How to start working with GTest and CMake. But, am facing the same issue.

user2991421
  • 387
  • 2
  • 4
  • 14
  • You already have `gtest` library target defined (comment in your code: "This defines the gtest and gtest_main targets."). Why do you search `gtest` with `find_library`? – Tsyvarev Jul 04 '18 at 10:13
  • My concern is that even though I've a gtest target defined, why does find_library return GTEST_LIB not found? – user2991421 Jul 05 '18 at 17:10
  • `find_library` searched a library file in the directories - system-specific ones and ones provided by the project. Directory `${CMAKE_BINARY_DIR}/googletest- build` corresponds none of them, so CMake doesn't search the library there. – Tsyvarev Jul 05 '18 at 19:26
  • make sense, thank you. Assuming that I've my gtest already installed in `C:\Users\username\Desktop\gtest\googletest\googletest` that contains several directories like build,include,src. How do I make sure that `find_library` can locate gtest? I mean what path variables do I need to set and where exactly? – user2991421 Jul 06 '18 at 04:03
  • Oh, and more important reason to not use `find_library` in the given case: the `gtest` library file **isn't created yet** on *configuration stage*, when `find_library` is executed. So it is impossible to find the library with given way. What is known at that stage, it is `gtest` library **target** corresponding to the library which will be **built later**, on *build stage*. – Tsyvarev Jul 06 '18 at 08:04
  • okay, I removed the reference to `find_library`. I get this error message: add_library cannot create target gtest because an imported target with the same name already exists. add_library cannot create target gtest_main because an imported target with the same name already exists. Cannot specify link libraries for target gtest_main which is not built by this project. Do you know how to fix this? – user2991421 Jul 06 '18 at 17:04
  • In the code you show us I see nothing which would create imported `gtest` target. Probably, you are trying to run another code? Also, you may try to remove remove all files in build directory and run `cmake` again. – Tsyvarev Jul 08 '18 at 07:47

0 Answers0