Fail to find gtest inside a container

*爱你&永不变心* 提交于 2020-12-15 05:16:05

问题


I am working inside a container. I want to try gtest so first I installed it by doing this inside the container:

  1. Download the source file from github/google/googletest
  2. build the project by cmake CMakeLists.txt
  3. call make
  4. cd lib and cp * /usr/lib
  5. cd googlemock/include and cp -r gmock /usr/local/include
  6. cd googletest/include and cp -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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!