首页 > 解决方案 > 无法在容器内找到 gtest

问题描述

我在一个容器内工作。我想尝试 gtest 所以首先我通过在容器中执行此操作来安装

  1. 从 github/google/googletest 下载源文件
  2. 构建项目cmake CMakeLists.txt
  3. 称呼make
  4. cd libcp * /usr/lib
  5. cd googlemock/includecp -r gmock /usr/local/include
  6. cd googletest/includecp -r gtest /usr/local/include

在此之后,我创建了一个 CMakeLists.txt 文件作为

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)

有了这个,我可以毫无问题地使用 gtest。(main.cpp 包含一些测试)

然后我想对我拥有的 ROS 项目做同样的事情,所以在创建了一个工作区和一个依赖于 gtest 的包之后,我做了catkin_make,我得到了

- +++ 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

在这种情况下,CMakeLists.txt 以

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
)

我想知道为什么它在这种情况下不起作用但在另一种情况下起作用?

标签: c++cmakecontainersros

解决方案


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


推荐阅读