1

我正在尝试组合两个 CMakeLists.txt 文件来编译一个同时具有 ROS 和 Libtorch 依赖项的 C++ 程序。下面提供了各个文件:

Libtorch CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)

# The following code block is suggested to be used on Windows.
# According to https://github.com/pytorch/pytorch/issues/25457,
# the DLLs need to be copied to avoid memory errors.
if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET example-app
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:example-app>)
endif (MSVC)

我在这里找到了这个:https ://pytorch.org/cppdocs/installing.html

ROS CMakeListis.txt 文件:

cmake_minimum_required(VERSION 2.8.3)
project(fly_bot_cpp)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  geometry_msgs
  tf
  gazebo_msgs
)

include_directories(
 include
  ${catkin_INCLUDE_DIRS}
  ${Boost_INCLUDE_DIRS}
)

add_executable(example src/example.cpp)
target_link_libraries(example ${catkin_LIBRARIES})

程序 example-app.cpp 有 ROS 和 LibTorch 两个库。

所以这是我试图做的:

cmake_minimum_required(VERSION 2.8.3)
project(fly_bot_cpp)

set(CMAKE_PREFIX_PATH="home/jarvis/libtorch;/opt/ros/melodic")

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
  geometry_msgs
  tf
  rospy
  message_generation
)

find_package(Torch REQUIRED)

include_directories(
 include
  ${catkin_INCLUDE_DIRS}
  ${Boost_INCLUDE_DIRS}
)

add_executable(test_quad src/test_quad.cpp)
target_link_libraries(test_quad ${catkin_LIBRARIES} "${TORCH_LIBRARIES}")

set_property(TARGET test_quad PROPERTY CXX_STANDARD 14)

代码test_quad.cpp(以前称为example-app.cpp)包含ros头文件和torch头文件:

#include "ros/ros.h"
#include <torch/torch.h>
.
.
.

但是,我收到以下错误。

fatal error: torch/torch.h: No such file or directory
 #include <torch/torch.h>
          ^~~~~~~~~~~~~~~
compilation terminated.

有人可以帮我吗?

太感谢了。

4

1 回答 1

0

删除 target_link_libraries 中 ${TORCH_LIBRARIES} 周围的引号。该行应如下所示

target_link_libraries(test_quad ${catkin_LIBRARIES} ${TORCH_LIBRARIES})

这应该可以解决割炬头。

还请查看这篇文章,以找到更多关于您将来可能会陷入困境的答案-https://answers.ros.org/question/347885/combining-cmakeliststxt-of-libtorch-and-cmakeliststxt-of -ros-package/#

我希望这个答案对新加入 ros 和 libtorch 的人有所帮助:)

于 2020-12-17T00:38:57.287 回答