GoogleTest gtest_discover_test could not find test - c++

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!

Related

libxlsxwriter library - How to connect to personal project with CMake C++

How do I connect the libxlsxwriter library to my personal project using CMake.
I cloned the official library files from https://github.com/jmcnamara/libxlsxwriter.
Then I extracted it in my personal project and added a few lines of CMake:
cmake_minimum_required(VERSION 3.11.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
[enter image description here][1]
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main # release-1.10.0
)
include(FetchContent)
FetchContent_Declare(
Bcrypt
GIT_REPOSITORY https://github.com/veltro123/Bcrypt.cpp
GIT_TAG master
)
FetchContent_MakeAvailable(Bcrypt)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
# Now simply link against gtest or gtest_main as needed. Eg
#ADDED
include_directories("libxlsxwriter/include")
set(XLSXWRITER_LIBRARY "libxlsxwriter/build/libxlsxwriter.a")
add_library(xlsxwriter STATIC IMPORTED)
set_target_properties(xlsxwriter PROPERTIES IMPORTED_LOCATION ${XLSXWRITER_LIBRARY})
target_link_libraries(${PROJECT_NAME} xlsxwriter)
#END
project(MONEYTRACKER)
enable_testing()
add_executable(${PROJECT_NAME} main.cpp Src/Transaction.cpp Src/Date.cpp Src/User.cpp)
add_executable(${PROJECT_NAME}-ut Testing/DateTests.cpp Testing/UserTests.cpp Src/Transaction.cpp Src/Date.cpp Src/User.cpp)
target_link_libraries(${PROJECT_NAME}-ut gtest_main)
target_link_libraries(${PROJECT_NAME} PRIVATE bcrypt)
include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME}-ut)
This is the error I get:

Creating c++ library using cmake

Dear cmake / c++ experts,
I created a small library for display and input on the console. Now I would like to use it in another project. Unfortunately, even after quite some time spent in the cmake documents, I cannot get it to work.
The library is here: https://github.com/HEIGVD-PRG1-F-2022/prg1f-io and uses the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(prg1f-io VERSION 0.1 DESCRIPTION "Input and display methods for PRG1-F")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
add_library(prg1f-io STATIC src/display.cpp)
set_target_properties(prg1f-io PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties(prg1f-io PROPERTIES PUBLIC_HEADER include/display.h)
include(GNUInstallDirs)
install(TARGETS prg1f-io
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
And I created a small test here: https://github.com/HEIGVD-PRG1-F-2022/prg1f-test with the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(prg1f_test)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(prg1f-io
GIT_REPOSITORY https://github.com/HEIGVD-PRG1-F-2022/prg1f-io.git
GIT_TAG main
)
FetchContent_MakeAvailable(prg1f-io)
add_executable(prg1f-test main.cpp)
target_link_libraries(prg1f-test PRIVATE prg1f-io)
While the CMakeLists.txt in the test does download the library and compiles it, I cannot get access to the header files in the test. I tried different includes, but none of the following work:
#include <prg1f-io/include/display.h>
#include <prg1f-io/display.h>
#include <prg1f-io>
#include <display.h>
I'm sure it's a little line somewhere in one of the CMakeLists.txt, but I cannot find it.
Any help is greatly appreciated!
Thanks to #Tsyvarev, I can now rest in peace:
The CMakeLists.txt of the library:
cmake_minimum_required(VERSION 3.23)
project(prg1f-io VERSION 0.1 DESCRIPTION "Input and display methods for PRG1-F")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
add_library(prg1f-io STATIC src/display.cpp)
set_target_properties(prg1f-io PROPERTIES VERSION ${PROJECT_VERSION})
target_include_directories(prg1f-io PUBLIC include)
And the CMakeLists.txt of the binary using the library:
cmake_minimum_required(VERSION 3.23)
project(prg1f_test)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(prg1f-io
GIT_REPOSITORY https://github.com/HEIGVD-PRG1-F-2022/prg1f-io.git
GIT_TAG main
)
FetchContent_MakeAvailable(prg1f-io)
add_executable(prg1f-test main.cpp)
target_link_libraries(prg1f-test PRIVATE prg1f-io)
Now I can include the headers using:
#include <display.h>
All I'm missing now is how to #include<prg1f-io/display.h> without having to add a prg1f-io/include/prg1f-io/display.h directory.

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.

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

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
)