2

I am trying to combine two CMakeLists.txt files to compile a C++ program that has both ROS and Libtorch dependencies. The individual files are provided below:

Libtorch CMakeLists.txt file:

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)

I found this here: https://pytorch.org/cppdocs/installing.html

ROS CMakeListis.txt file:

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})

The program example-app.cpp has both libraries of ROS and LibTorch.

So here is what i tried to do:

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)

The code test_quad.cpp (previously referred to as example-app.cpp) contains ros header files and torch header files:

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

However, I get the following error.

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

Can someone please help me out??

Thank you so much.

6
  • The most direct approach would be compining lines from the two CMakeLists.txt into the one CMakeLists.txt. Have you tried that? What is you problem? Commented Apr 26, 2020 at 10:37
  • Please show what you've already tried and whats the exact problem with it. Commented Apr 26, 2020 at 12:33
  • @Tsyvarev Sir, I have edited my post. I am a newbie to CMake so please forgive me if I have done anything silly. Thank you so much for your precious time. Commented Apr 26, 2020 at 18:42
  • @Fruchtzwerg Sir, I have edited my post. Thank you so much for your precious time. Commented Apr 26, 2020 at 18:43
  • Example of using tourch sets CMAKE_CXX_FLAGS variable, but your CMakeLists.txt doesn't contain this setting. Commented Apr 26, 2020 at 21:49

1 Answer 1

1

Remove the quotes around ${TORCH_LIBRARIES} in your target_link_libraries. The line should look like this

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

This should resolve the torch header.

Please also take a look at this post to find some more answers to where you might get stuck in the future-https://answers.ros.org/question/347885/combining-cmakeliststxt-of-libtorch-and-cmakeliststxt-of-ros-package/#

I hope this answer is helpful to someone new coming onboard to ros and libtorch :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.