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?
- 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"
- 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