【debug】ROS2 find_package(XXX REQUIRED)找不到库的解决方法

我需要用到glog库,通过 sudo apt-get install libgoogle-glog-dev 已经安装了glog。

但是在CMakeLists.txt中,find_package(glog REQUIRED)时,报错:

CMake Error at CMakeLists.txt:15 (find_package):
  By not providing "Findglog.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "glog", but
  CMake did not find one.

  Could not find a package configuration file provided by "glog" with any of
  the following names:

    glogConfig.cmake
    glog-config.cmake

  Add the installation prefix of "glog" to CMAKE_PREFIX_PATH or set
  "glog_DIR" to a directory containing one of the above files.  If "glog"
  provides a separate development package or SDK, be sure it has been
  installed.

通过在计算机中查找 "Findglog.cmake" 发现确实找不到这个.cmake文件。

此时可以通过手动查找glog的include头文件目录,以及.so,.a动态链接库,然后链接到CMakeLists.txt中即可。

还是以glog为例,在计算机中查找glog,找到 /usr/include 中包含了glog:(一般头文件都在 /usr/include中)

找到 /usr/lib/x86_64-linux-gnu/ 中包含了 libglog.so 和 libglog.a(.so是动态链接库,.a是静态链接库)

因此在CMakeLists.txt中增加下面两行:

include_directories(
        include
        /usr/include/glog
)
target_link_libraries(${PROJECT_NAME}
        /usr/lib/x86_64-linux-gnu/libglog.so
        /usr/lib/x86_64-linux-gnu/libglog.a
        )

其中 include_directories () 里增加 /usr/include/glog 即可。


对比原来的 CMakeLists.txt 与修改后的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(fr07)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)

# Find ROS 2 packages
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(ray_msgs REQUIRED)
include_directories(
        include
)

# Add an executable
add_executable(${PROJECT_NAME}
        src/main.cpp
        src/communicationProcess.cpp
        src/dataDownload.cpp
        src/udpCommunication.cpp
)
# Link dependencies
ament_target_dependencies(${PROJECT_NAME} rclcpp ray_msgs )

# Install the executable
install(TARGETS ${PROJECT_NAME}
        DESTINATION lib/${PROJECT_NAME})

#install(DIRECTORY launch/
#  DESTINATION share/${PROJECT_NAME}/launch)

ament_package()
cmake_minimum_required(VERSION 3.15)
project(fr07)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)

# Find ROS 2 packages
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(ray_msgs REQUIRED)
include_directories(
        include
        /usr/include/glog
)

# Add an executable
add_executable(${PROJECT_NAME}
        src/main.cpp
        src/communicationProcess.cpp
        src/dataDownload.cpp
        src/udpCommunication.cpp
)
target_link_libraries(${PROJECT_NAME}
        /usr/lib/x86_64-linux-gnu/libglog.so
        /usr/lib/x86_64-linux-gnu/libglog.a
        )
# Link dependencies
ament_target_dependencies(${PROJECT_NAME} rclcpp ray_msgs )

# Install the executable
install(TARGETS ${PROJECT_NAME}
        DESTINATION lib/${PROJECT_NAME})

#install(DIRECTORY launch/
#  DESTINATION share/${PROJECT_NAME}/launch)

ament_package()

增加了13行,23-26行。

猜你喜欢

转载自blog.csdn.net/qq_45461410/article/details/133930571