问题
I am working inside a container. I want to try gtest so first I installed it by doing this inside the container:
- Download the source file from github/google/googletest
- build the project by
cmake CMakeLists.txt
- call
make
cd lib
andcp * /usr/lib
cd googlemock/include
andcp -r gmock /usr/local/include
cd googletest/include
andcp -r gtest /usr/local/include
After this I created a CMakeLists.txt file as
cmake_minimum_required(VERSION 3.13)
set(CMAKE_CXX_STANDARD 11)
find_package(GTest REQUIRED)
message("GTest_INCLUDE_DIRS = ${GTest_INCLUDE_DIRS}")
add_executable(myExecutable main.cpp)
target_link_libraries(myExecutable ${GTEST_LIBRARIES} pthread)
With this, I could use gtest without problem. (main.cpp contained some tests)
Then I want to do the same for a ROS project I have, so after creating a workspace and a package that depends on gtest I did catkin_make
and I get
- +++ processing catkin package: 'ros_gtest'
-- ==> add_subdirectory(ros_gtest)
-- Could NOT find GTest (missing: GTest_DIR)
-- Could not find the required component 'GTest'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found.
CMake Error at /opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
Could not find a package configuration file provided by "GTest" with any of
the following names:
GTestConfig.cmake
gtest-config.cmake
Add the installation prefix of "GTest" to CMAKE_PREFIX_PATH or set
"GTest_DIR" to a directory containing one of the above files. If "GTest"
provides a separate development package or SDK, be sure it has been
installed.
Call Stack (most recent call first):
ros_gtest/CMakeLists.txt:10 (find_package)
-- Configuring incomplete, errors occurred!
See also "/root/ros_gtest_example/build/CMakeFiles/CMakeOutput.log".
See also "/root/ros_gtest_example/build/CMakeFiles/CMakeError.log".
make: *** [Makefile:320: cmake_check_build_system] Error 1
In this case the CMakeLists.txt start as
cmake_minimum_required(VERSION 3.0.2)
project(ros_gtest)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
GTest
roscpp
rospy
std_msgs
)
I wonder why it does not work in this case but it works in the other?
回答1:
missing make install
and you don't copy any gtest config file...
IMHO, You should use FetchContent()
or/and learn how CMake find_package() work...
include(CTest)
if(BUILD_TESTING)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
endif()
ref: mizux/cmake-cpp
来源:https://stackoverflow.com/questions/64968933/fail-to-find-gtest-inside-a-container