Why does "#include <gtest/gtest.h>" fail even after having used "FetchContent_MakeAvailable" in my CMake file? - c++

This is my first time attempting the Google Test API in C++ and one of my first experiences with CMake. If it's useful, I'm using CLion. My CMake file, shown below, should ostensibly allow for files to #include <gtest/gtest.h>:
cmake_minimum_required(VERSION 3.19)
project(chord)
# GoogleTest requires at least C++11
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread")
add_executable(
chord
main.cpp
src/node.h
src/node.cpp
src/log.h
src/log.cpp
src/sha1.h
src/sha1.cpp
src/messageParser.h
src/messageParser.cpp
src/fingerTable.h
src/fingerTable.cpp
src/key.h
src/key.cpp)
include(GoogleTest)
enable_testing()
add_executable(
unitTests
test/fingerTableTests.cc
test/keyTests.cc
test/logTests.cc
test/messageParserTests.cc
test/nodeTests.cc
test/messageParserTests.cc
)
target_link_libraries(
unitTests
gtest_main
)
include(GoogleTest)
gtest_discover_tests(unitTests)
However, when I attempt #include <gtest/gtest.h> from main.cpp, I receive the error gtest/gtest.h: No such file or directory. Can someone pinpoint the error in my CMake file? Thanks and sorry for the trouble.

Try to also link gtest:
target_link_libraries(
unitTests
gtest_main
gtest
)

Related

GoogleTest gtest_discover_test could not find test

I'm trying to add google test to my project and keep getting
"[build] get_property could not find TARGET testcolor. Perhaps it has not yet been
"
This is my cmake file:
cmake_minimum_required(VERSION 3.13)
project(RAYTRACE)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
include(CTest)
enable_testing()#Redundent I know
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
include_directories("<...>/googletest-src/googletest/include")
##GTEST##
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
set(SOURCE_FILES
main.cpp
src/Utils/testcolor.cc
src/Utils/testcolor
)
add_executable(RAYTRACE ${SOURCE_FILES})
target_link_libraries(RAYTRACE
PRIVATE
testcolor
GTest::gtest_main
GTest::gtest
)
include(GoogleTest)
gtest_discover_tests(testcolor)
Without the last line gtest is built properly and the gtest.h header is included in "src/Utils/testcolor.cc" and detected.
yet I cant add the test.
I solved the problem by following the This
guide online which made me undersand my many many mistakes configuring CMake and GTest. Thanks!

CMake not including library files when I use FetchContent

I'm working on a project where the CMake structure looks like this:
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
set(CMAKE_CXX_STANDARD 20)
project(overflow VERSION 0.1.0)
add_subdirectory(src)
enable_testing()
include(CTest)
add_subdirectory(test)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
src/CMakeLists.txt
find_package(Boost REQUIRED)
set(HEADER_FILES overflow.h)
include_directories(${Boost_INCLUDE_DIRS})
add_library(overflow ${HEADER_FILES})
link_libraries(overflow PRIVATE Boost::uuid) # Fails with `target_`
These work fine. However, I get an error when I set up GTest as shown below in my test directory.
test/CMakeLists.txt
## Setup GTest
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/e2239ee6043f73722e7aa812a459f54a28552929.zip
)
### For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_executable(overflowtest overflow.i.t.cpp)
target_link_libraries(overflowtest PRIVATE
overflow
gtest
gtest_main)
include(GoogleTest)
gtest_discover_tests(overflowtest)
CMake builds correctly, but when I run the test, the error in overflow.h is:
boost/uuid/uuid.hpp: No such file or directory
What I find strange is that if I change my settings so that test/CMakeLists.txt looks like:
test/CMakeLists.txt
find_package(GTest CONFIG REQUIRED)
add_executable(overflowtest overflow.i.t.cpp)
target_link_libraries(overflowtest PRIVATE
overflow
GTest::gtest
GTest::gtest_main)
There are no errors and it runs correctly. Can someone please explain why using the FetchContent method recommended by Google seems to break my project?

CMake 3.22.4 does not compatible with GTest 1.10.0.20201025-1.1

I've build my unit test code on Ubuntu 21.10, CMake 3.18.4 and GTest 1.10.0.20201025-1.1.
I wrote CMakeList.txt file as this.
# The minimum version of CMake Required
cmake_minimum_required (VERSION 2.8.12)
# Any project name will suffice, this has connotaions when using advanced CMake Features
set(PROJECT_NAME tests)
project (${PROJECT_NAME})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Just in case someone had the include files in seperate directory
include_directories(../include)
include_directories(..)
# This uses the complete student's library
aux_source_directory(.. SRC_LIST)
list(REMOVE_ITEM SRC_LIST "../main.cpp")
message ( STATUS "Compiling test_lib with following files ${SRC_LIST}" )
add_library(test_lib ${SRC_LIST})
# Now we make the gtests
set(GTEST_ROOT "/usr/src/gtest" CACHE PATH "Path to googletest")
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
if(NOT GTEST_LIBRARY)
message("GTest library not found")
endif()
add_executable(rawTests test_rawdata.cpp)
target_link_libraries(rawTests ${GTEST_LIBRARIES} pthread)
target_link_libraries(rawTests test_lib)
add_executable(timeTests test_time.cpp)
target_link_libraries(timeTests ${GTEST_LIBRARIES} pthread)
target_link_libraries(timeTests test_lib)
It works properly on my end.
But when I deliver this to my friend who uses CMake 3.22.4, it throws error look like this
Error Image
It's kind of weird issue and I didn't ever faced this sort of issue before.
I wonder anybody who has deep knowledge for CMake and GTest can help me to handle this.
Thank you in advance.

No tests found while running gtest

I was making use of gtest to test my c++ project. I followed the steps from here
Everything went well but when I ran the cd build && ctest command i'm getting the output as Test project C:/Users/admin/Desktop/last/build No tests were found!!!
My CMakeLists.txt file
cmake_minimum_required(VERSION 3.14)
project(last)
# GoogleTest requires at least C++11
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(
hello_test
fact.cpp
header.h
main.cpp
testt.cpp
)
target_link_libraries(
hello_test
gtest_main
)
include(GoogleTest)
gtest_discover_tests(hello_test)
I changed a line in my CMakeLists file
add_executable(
hello_test
header.h
main.cpp
)
And added a line
add_test(testt.cpp hello_test)
This gave me the output correctly and ran the tests

CMake does not build the added sub-directory first

I am writing a C++ project that uses Poco Net library. I use CMake to configure the project.
I would like to add Poco as a sub-directory to my project so that it is built in my main project. Here is my shortened main CMakeLists.txt
cmake_minimum_required(VERSION 3.2)
project(FunProj)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode...")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(HEADER_FILES IDataProvider.h DataProvider.h)
set(SOURCE_FILES main.cpp
DataProvider.cpp)
set(POCO_STATIC ON)
ADD_SUBDIRECTORY(poco)
include_directories(${CMAKE_SOURCE_DIR}/poco/Net/include)
include_directories(${CMAKE_SOURCE_DIR}/poco/Foundation/include)
link_directories(${CMAKE_CURRENT_BINARY_DIR}/poco/lib)
add_executable(FunProj ${SOURCE_FILES} ${HEADER_FILES})
target_link_libraries(${EXEC_NAME} PocoNet)
When I run cmake it configures everything including Poco but when I run make it does not compile the Poco libraries. It only compiles the main.o and DataProvider.o and then the linker fails with an error that libPocoNet.a does not exist.
What is the problem and how may one solve it?
Thank you.