c++ - 无法在容器内找到 gtest
问题描述
我在一个容器内工作。我想尝试 gtest 所以首先我通过在容器中执行此操作来安装它:
- 从 github/google/googletest 下载源文件
- 构建项目
cmake CMakeLists.txt
- 称呼
make
cd lib
和cp * /usr/lib
cd googlemock/include
和cp -r gmock /usr/local/include
cd googletest/include
和cp -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
)
我想知道为什么它在这种情况下不起作用但在另一种情况下起作用?
解决方案
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
推荐阅读
- python - pyfirmata 中伺服运动之间的延迟不起作用
- unity3d - Unity 匿名序列化游戏对象中的所有组件
- unity3d - Unity:如何在 Unity 中获取排行榜分数的元数据标签?
- c# - 在 c# 中加载某些内容并且其他窗口保持禁用状态时如何使用循环进度
- android - 如何管理和定义我们从应用程序发送的所有数据分析?
- swift - 使用“@objc”属性覆盖的扩展方法背后的机制是什么?
- java - 字符串查询验证 json 数据
- vmware - VMWare 跨数据存储共享 SAN 卷。可能吗?
- git - 我可以从一个绝对没有安装 Git 命令的简单文件服务器克隆吗?
- android - 无法在 recyclerview 适配器类中解决