组合两个CMakeLists.txt文件(ROS和Libtorch)



我正试图将两个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(以前称为示例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.

有人能帮帮我吗??

非常感谢。

删除target_link_LIBRARIES中${TORCH_LIBRARIES}周围的引号。这条线应该像这个

target_link_libraries(test_quad${catkin_LIBRARES}${TORCH_libraries}(

这应该可以解决火炬头。

也请看一下这篇文章,找到更多关于你未来可能陷入困境的答案-https://answers.ros.org/question/347885/combining-cmakeliststxt-of-libtorch-and-cmakeliststxt-of-ros-package/#

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

最新更新