CMake Error: Unable to find package GTest [duplicate] - c++

This question already has answers here:
How to start working with GTest and CMake
(11 answers)
Closed 2 years ago.
I am developing a C/C++ app using CMake. I want to use GTest in my project for unit testing. For this I have decided to use GTest as a git submodule in my repository.
My directory hierarchy is as follows:
/
--include
--lib
--GoogleTest
--src
--Tests
The GoogleTest subdirectory in lib contains the source code of GTest from their official repository.
But I am unable to use it to test my source. The CMakeLists.txt file at the root of my repository is as follows:
OPTION (BUILD_UNIT_TESTS "Build unit tests" ON)
if (BUILD_UNIT_TESTS)
enable_testing ()
find_package (GTest REQUIRED)
add_subdirectory (Tests)
endif ()
But I receive the error:
Error:Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR
GTEST_MAIN_LIBRARY)
When I searched for this, there were many questions similar to mine, but none of them addressed my problem. And their manual is very limited and does not tell much about its usage properly.
CMake is successfully able to build the target GTest but fails to recognise it when I try to use it as an external package.

You don't have to manually download the Gtest git. Just add the following lines to your CMakeLists.txt
# -------- GOOGLE TEST ----------
# 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_CURRENT_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_CURRENT_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_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_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()
# -------------------------------------------------------------------------
enable_testing()
include_directories("${gtest_SOURCE_DIR}/include")
And add the following lines to another file, named CMakeLists.txt.in, in the same directory your CMakeLists.txt file is
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 release-1.10.0
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
What does it do ?
The things you added to your main CMakeLists will download GTest and GMock git projects at the version specified in the CMakeLists.txt.in file. Then it will build these, and include the build path in your main project.
Source : GoogleTest Git

Related

CMake error "Automatic include detection via cmake could not be executed. See log for details."

I am trying to use the google test framework with my current eclipse project, which builds using CDT builder with cmake4eclipse 3.0.3. For including the googletest package, I used the following to download it as an external project within my project. The following is my CMakeLists.txt.in file:
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
And this is the CMakeLists.txt in which I actually build the google test and include it for my tests:
project(Myproject)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
##################### build google test #####################
# 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_CURRENT_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_CURRENT_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_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_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 ${gtest_SOURCE_DIR})
endif()
#######################################################################################
# Some irrelevant code here ...
#######################################################################################
############## Build my test ###################
add_executable(unit_test src/SomeUnitTest.cpp)
target_link_libraries(unit_test gtest gtest_main)
add_test(NAME big_test COMMAND unit_test)
My output is:
[cmake]: -- Configuring done
[cmake]: -- Generating done
[cmake]: -- Build files have been written to: C:/Users/Me/eclipse-workspace/Myproject/intermediate/cmake/AXCF2152,22.3.0.20/Release/googletest-download
[cmake]: [ 11%] [34m[1mPerforming update step for 'googletest'[0m
[cmake]: Current branch main is up to date.
[cmake]: [ 22%] [34m[1mNo configure step for 'googletest'[0m
[cmake]: [ 33%] [34m[1mNo build step for 'googletest'[0m
[cmake]: [ 44%] [34m[1mNo install step for 'googletest'[0m
Automatic include detection via cmake could not be executed. See log for details.
Successfully generated all files with the 'code' generator for C:\Users\Me\eclipse-workspace\Myproject.
I am using a Windows machine and as I mentioned above my eclipse is using the CDT builder with cmake4eclipse 3.0.3. When I query the cmake version it tells me it is 3.13.1. I do have another cmake version 3.23.2 installed on my machine as well.
I could not find any info on what the cmake error "Automatic include detection via cmake could not be executed. See log for details." means, I could also not find any relevant info about it in my CMakeOutput.log. What is going on?

Integrate pre-compiled libraries into C++ codebase with CMake ExternalProject

I want to integrate CasADi into a CMake-based C++ codebase as an ExternalProject. For this purpose, I would like to use pre-compiled libraries because building from source is not recommended. So far, I have only managed to write the following:
ExternalProject_Add(
casadi-3.5.5
URL https://github.com/casadi/casadi/releases/download/3.5.5/casadi-linux-py39-v3.5.5-64bit.tar.gz
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
PREFIX ${CMAKE_BINARY_DIR}/external/casadi)
and I noticed that all the binaries are correctly downloaded in the specified folder. However, I do not know how to link my targets to CasADi, nor how to find the package.
There is a natural problem with ExternalProject_Add:
ExternalProject_Add executes commands only on build.
Hence, download will not happen at the configure stage of your project which makes it difficult to use find_package, because the files cannot be found during your first configure run.
Take this CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(untitled)
set(CMAKE_CXX_STANDARD 17)
add_executable(untitled main.cpp)
include(ExternalProject)
ExternalProject_Add(
casadi-3.5.5
URL https://github.com/casadi/casadi/releases/download/3.5.5/casadi-linux-py39-v3.5.5-64bit.tar.gz
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
PREFIX ${CMAKE_BINARY_DIR}/external/casadi)
find_package(casadi HINTS ${CMAKE_BINARY_DIR}/external/casadi/src/casadi-3.5.5/casadi)
target_link_libraries(untitled casadi)
In order to use it you have to do the following:
Configure your project
mkdir build
cd build
cmake ..
Build (download) casadi-3.5.5
cmake --build . --target casadi-3.5.5
Reconfigure your project, because now find_package will find the needed files
cmake ..
Build your targets
cmake --build .
If you want a one step build, there are ways to get around this problem
Use FetchContent
Create a sub-cmake-project in a subfolder with all the ExternalProject_Add commands and execute the approriate build (download) steps manually in your own CMakeLists.txt via execute_process calls: https://stackoverflow.com/a/37554269/8088550
Here is an example for the second option, which might be better since FetchContent doesn't have the full functionality of ExternalProject.
main.cpp
#include <casadi/casadi.hpp>
int main()
{
casadi_printf("This works!");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(untitled)
set(CMAKE_CXX_STANDARD 17)
# some default target
add_executable(untitled main.cpp)
# Configure and build external project
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/external)
execute_process(
COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR}/external
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/external
)
execute_process(
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/external
)
# find and link externals
find_package(casadi REQUIRED HINTS ${CMAKE_BINARY_DIR}/external/external/casadi/src/casadi-3.5.5/casadi)
target_link_libraries(untitled casadi)
external/CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(external)
include(ExternalProject)
ExternalProject_Add(
casadi-3.5.5
URL https://github.com/casadi/casadi/releases/download/3.5.5/casadi-linux-py39-v3.5.5-64bit.tar.gz
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
PREFIX ${CMAKE_BINARY_DIR}/external/casadi)
The point is to have another cmake project under external/CMakeLists.txt, which gets configured and build via execute_process calls from the main cmake project. Do note, that you can now have find_package(casadi REQUIRED ...) at configure stage, because the download will happen just before.

Dealing with nested cmake projects using FetchContent_

I am looking to automate the compilation of a given c++ library (in this case, cpprestsdk). I am looking to build the library using cmake.
Like many other projects, this has dependencies. Namely, it requires OpenSSL, Boost, ZLIB and websocketpp.
I'm looking for a way in which I can provide a CMakeLists file that will fetch and build all of the prerequisites and allow cpprestsdk to build without necessarily having the libraries pre-installed on a computer.
The way I am fetching cpprestsdk is as follows:
FetchContent_Declare(cpprestsdk
GIT_REPOSITORY https://github.com/microsoft/cpprestsdk.git
GIT_TAG master)
FetchContent_GetProperties(cpprestsdk)
if(NOT cpprestsdk_POPULATED)
FetchContent_Populate(cpprestsdk)
add_subdirectory(${cpprestsdk_SOURCE_DIR} ${cpprestsdk_BINARY_DIR})
endif()
Of course, since this does not populate it's own dependencies, it will not be able to build. Although websocketpp has an embedded release, it is only used if not found. For this reason, I will use it as an example.
FetchContent_Declare(websocketpp
GIT_REPOSITORY https://github.com/zaphoyd/websocketpp.git
GIT_TAG master
)
FetchContent_GetProperties(websocketpp)
if(NOT websocketpp_POPULATED)
FetchContent_Populate(websocketpp)
add_subdirectory(${cpprestsdk_SOURCE_DIR} ${cpprestsdk_BINARY_DIR})
endif()
This will fetch websocketpp, but as it is only the configure stage, nothing will be built. For this reason, cpprestsdk will not be able to find the dependency and instead fallback to the embedded release.
My question is therefore: Is there any way I can force a build after fetching a package? Or remove the library requirement during the configure stage? I think this may be possible using ExternalProject instead, however I'm not too sure how I would set that up either.
I use a subroutine to fetch the source code, then use execute_process to build and install the target.
After this, even find_package can locate the library.
Fetch subroutine:
cmake_minimum_required(VERSION 3.16)
# suppress CMP0042
cmake_policy(SET CMP0042 NEW)
set(CMAKE_MACOSX_RPATH 1)
# this must exist or cmake will complain about not presenting a project()
project(${PK_NAME}_download NONE)
include(FetchContent)
FetchContent_Declare(
${PK_NAME}
GIT_REPOSITORY "${PK_GIT}"
GIT_TAG "${PK_GIT_TAG}"
PREFIX "${#PK_NAME#_ROOT}"
)
if(NOT ${PK_NAME}_POPULATED)
FetchContent_MakeAvailable(${PK_NAME})
endif()
Download, build and install:
# copy the template to place
configure_file(${CMAKE_CURRENT_LIST_DIR}/FetchLib.cmake ${${PK_NAME}_ROOT}/CMakeLists.txt)
# download source code
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" -DCMAKE_INSTALL_PREFIX:PATH=${${PK_NAME}_ROOT} ${SUB_PARAM}
.
RESULT_VARIABLE result
WORKING_DIRECTORY ${${PK_NAME}_ROOT}
)
if(result)
message(FATAL_ERROR "CMake step for ${PK_NAME} failed: ${result}")
endif()
set(CMAKE_INSTALL_PREFIX "${${PK_NAME}_ROOT}")
# build
execute_process(
COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${${PK_NAME}_ROOT}
)
if(result)
message(FATAL_ERROR "Build step for ${PK_NAME} failed: ${result}")
endif()
# install
execute_process(
COMMAND ${CMAKE_COMMAND} --install .
RESULT_VARIABLE result
WORKING_DIRECTORY ${${PK_NAME}_ROOT}
)
if(result)
message(FATAL_ERROR "Install step for ${PK_NAME} failed: ${result}")
endif()
You are looking for ExternalProject_add
I often use this approach to build externals projects, it provides me automatic control over external projects that are inserted in my projects.
You can include like this:
ExternalProject_add(cpprestsdkDownload
GIT_REPOSITORY https://github.com/microsoft/cpprestsdk
GIT_TAG master
CMAKE_ARGS
-DWERROR:BOOL="0"
-DBUILD_SAMPLES:BOOL="0"
-DBUILD_TESTS:BOOL="0" )
You can use your custom configure,build and install commands to ! Have Fun !

GTEST_LIB not found

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.

Why does CMake not find GTest (Google Test)?

There is a ready project. In one of the cmake-files there is a source code:
find_package(GTest REQUIRED)
if (NOT GTest_FOUND)
message(FATAL_ERROR "Cannot find Google Test Framework!")
endif()
Gives an error: "Cannot find Google Test Framework!"
How to fix error?
The FindGTest.cmake file uses the environment variable GTEST_ROOT.
You can add this variable to your system or just add it to your CMakeLists.txt file:
set(GTEST_ROOT "c:/path/to/gtest/root" CACHE PATH "path to gtest").
This should solve your problem. It is of course possible to completely add gtest to a project (like Luis Miglietti suggested), but thats maybe not what you want to do as a first try.
This could be useful for you so you don't have to depend on a local google test install, this should work independently if you have google test installed in your machine
You can add this to your cmake file (you should take care of the proper linking / include depending on your project structure)
This will download google test, configure the installation and build it in vendor/gtm/gtest (you could always change this) inside your main build folder. Then you can link gtest to an executable so you can run your tests from there
include(ExternalProject)
find_package(Git REQUIRED)
# Build googletest
ExternalProject_Add(
googletest
PREFIX "vendor/gtm"
GIT_REPOSITORY "https://github.com/google/googletest.git"
GIT_TAG release-1.8.0
TIMEOUT 10
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
UPDATE_COMMAND ""
)
# Build gtest
ExternalProject_Add(
gtest_src
PREFIX "vendor/gtm"
SOURCE_DIR "vendor/gtm/src/googletest/googletest"
INSTALL_DIR "vendor/gtm/gtest"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/vendor/gtm/gtest
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
DOWNLOAD_COMMAND ""
UPDATE_COMMAND ""
)
# Prepare gtest
ExternalProject_Get_Property(gtest_src install_dir)
set(GTEST_INCLUDE_DIR ${install_dir}/include)
set(GTEST_LIBRARY_PATH ${install_dir}/lib/libgtest.a)
file(MAKE_DIRECTORY ${GTEST_INCLUDE_DIR})
add_library(gtest STATIC IMPORTED)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${GTEST_LIBRARY_PATH})
set_property(TARGET gtest APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR})
add_dependencies(gtest_src googletest)
add_dependencies(gtest gtest_src)
Then you can link gtest to an executable with something like
add_executable(tester test/tester.cc)
target_link_libraries(tester gtest)
enable_testing()
add_test(<library> tester)
While CMake provides a FindGTest.cmake module since 2009...
I prefer to incorporate googletest in your CMake project like explaining in the googletest documentation.
https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project
Note: a more detailed explanation http://crascit.com/2015/07/25/cmake-gtest/