0

I was trying to build GTest from source and then link my target to it using cmake. But I see this error

mygtest % cmake --build build
[ 50%] Building CXX object CMakeFiles/hello_test.dir/mytest.cpp.o
/path/to/test/mygtest/mytest.cpp:1:10: fatal error: 'gtest/gtest.h' file not found
#include <gtest/gtest.h>
         ^~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/hello_test.dir/mytest.cpp.o] Error 1
make[1]: *** [CMakeFiles/hello_test.dir/all] Error 2
make: *** [all] Error 2

My question is what may I miss here?

This is my CMakeList File

cmake_minimum_required(VERSION 3.14)
project(my_project)

set(CMAKE_CXX_STANDARD 14)

find_library(
  GTEST_MAIN
  gtest_main
  PATHS /path/to/googletest/build/lib/
  NO_DEFAULT_PATH
  )

enable_testing()

add_executable(
  hello_test
  mytest.cpp
)
target_link_libraries(
  hello_test
  ${GTEST_MAIN}
)

include(GoogleTest)
gtest_discover_tests(hello_test)

What did I try?

  1. Printing the target's link libraries
get_target_property(HELLO_TEST_LIBRARIES hello_test LINK_LIBRARIES)
include(CMakePrintHelpers)
cmake_print_variables(HELLO_TEST_LIBRARIES)

// OUTPUT
-- HELLO_TEST_LIBRARIES="/path/to/googletest/build/lib/libgtest_main.a"
  1. using find_package(GTest) and then linking GTest::gtest works, but I don't want to use a precompiled version -- it seems to cause the error "Unfound Symbol" as mentioned here
  • 1
    Don't use `find_library`... use `find_package(GTest REQUIRED)` and then link to `GTest::gtest` and `GTest::gtest_main`. – Alex Reinking May 23 '22 at 20:57
  • @Yinglao Liu, your CMakeLists is already written so that you expect a precompiled gtest (due to `find_library()`), but you miss injection of headers folder, and it's just old & fragile CMake style. So follow @Alex Reinking advice, and use `find_package()` & link to imported targets like a good citizen: https://cmake.org/cmake/help/latest/module/FindGTest.html – SpacePotatoes May 24 '22 at 11:26

1 Answers1

0

I am open to any other better answers, but would post my current solution for those who might be interested.

Thanks @Alex Reinking and @SpacePotatoes for comments. based on what's suggested, inserting the following piece of codes to your CMakeLists.txt would allow you to automate the process of downloading googletest and building from source.

FetchContent_Declare(
 googletest
 GIT_REPOSITORY    https://github.com/google/googletest.git
 GIT_TAG           main
 SUBBUILD_DIR ${PROJECT_BINARY_DIR}/googletest-subbuild
 BINARY_DIR ${PROJECT_BINARY_DIR}/googletest-build
 SOURCE_DIR ${PROJECT_BINARY_DIR}/googletest-src
 )

FetchContent_Populate(
  googletest
)

execute_process(COMMAND cmake -S. -B${googletest_BINARY_DIR} WORKING_DIRECTORY ${googletest_SOURCE_DIR})
execute_process(COMMAND cmake --build . WORKING_DIRECTORY ${googletest_BINARY_DIR})

set(GTEST_LIBRARY ${googletest_BINARY_DIR}/lib/libgtest.a)
set(GTEST_INCLUDE_DIR ${googletest_SOURCE_DIR}/googletest/include)
set(GTEST_MAIN_LIBRARY ${googletest_BINARY_DIR}/lib/libgtest_main.a)

find_package(GTest REQUIRED)

Then you might be able to link to your target with

target_link_libraries(<target> GTest::gtest_main)