Combining CMakeLists.txt of Libtorch and CMakeLists.txt of ROS package

asked 2020-03-30 08:50:34 -0500

Jarvis1997 gravatar image

updated 2020-03-30 22:16:07 -0500

Hello all. I have been struggling with this issue for quite some time, and i request you to please help me. I am fairly new to CMake, so if this is a basic and obvious question, please forgive me.

There is this package called LibTorch, which is a C++ version of PyTorch. As described here, we have to build the code referred to as example-app (in the link) by writing a CMakeLists.txt file. Here are the contents of that CMakeLists.txt file:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

find_package(Torch REQUIRED)

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

The example-app.cpp file is a simple C++ file:

#include <torch/torch.h>
#include <iostream>

int main() {
    torch::Tensor tensor = torch::rand({2, 3});
    std::cout << tensor << std::endl;

To build this code, consider the folder following hierarchy:


Within the folder, we have to do this:

mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
cmake --build . --config Release

where, libtorch is a library folder that contains all the necessary header files etc., and the "/absolute/path/to/libtorch" is the absolute path to where the libtorch library folder is placed. Now, to run the code, we have to go to the build directory and execute the executable generated and we get the output. This works perfectly fine for me. Now, I want to include this library in one of my ROS C++ programs present in the src directory of my package. For my convenience, i have placed the libtorch library folder inside the src folder of my package called "cart_pole", and the ROS C++ program that i want to run is called control_DQN.cpp .

My question is, how do i combine the contents of this CMakeLists.txt and the CMakeLists.txt present in my package directory? Here is my (failed attempt), and I am getting some errors.

cmake_minimum_required(VERSION 3.0)


find_package(catkin REQUIRED COMPONENTS
find_package(Torch REQUIRED)
add_executable(control_DQN src/control_DQN.cpp)
target_link_libraries(control_DQN ${catkin_LIBRARIES} ${TORCH_LIBRARIES})
set_property(TARGET control_DQN PROPERTY CXX_STANDARD 14)

Now, when I run the catkin_make command, I get this error:

make[2]: *** No rule to make target '/usr/lib/x86_64-linux-gnu/ torch', needed by '/home/jarvis/catkin_cpp_ws/devel/lib/cart_pole/control_DQN'.  Stop.
CMakeFiles/Makefile2:1731: recipe for target 'cart_pole/CMakeFiles/control_DQN.dir/all' failed
make[1]: *** [cart_pole/CMakeFiles/control_DQN.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

This error did not come when I built the example-app.cpp code mentioned earlier. So i suppose that ROS catkin_make is missing some paths maybe because i did not specify it in the CMAKE_PREFIX_PATH? The libconsole-bridge-dev is installed correctly and there is no ... (more)

edit retag flag offensive close merge delete


target_link_libraries(control_DQN "${catkin_LIBRARIES} ${TORCH_LIBRARIES}")

why do you add quotes (ie: ") around ${catkin_LIBRARIES} and ${TORCH_LIBRARIES}?

${TORCH_LIBRARIES} likely contains torch, while ${catkin_LIBRARIES} will contain (among other things) /usr/lib/x86_64-linux-gnu/ By adding the quotes, you force those together, resulting in /usr/lib/x86_64-linux-gnu/ torch as shown in the error message.

Remove the quotes and try again.

gvdhoorn gravatar image gvdhoorn  ( 2020-03-30 14:59:08 -0500 )edit

@gvdhoorn, you are correct, after removing the quotes the error is gone. However, there is another error that I am getting. I have edited my post accordingly. Thank you so much for your time!

Jarvis1997 gravatar image Jarvis1997  ( 2020-03-30 22:17:12 -0500 )edit

This is probably the case of wrong libtorch version.

In my case, I downloaded the wrong cuda libtorch. I downloaded 10.2 cuda libtorch 1.7.1 which gave the same error of undefined ros::init and a bunch of other similar errors. I checked my cuda version with nvidia-smi command and downloaded cuda 11 version libtorch(1.7.1)

So give it a try. it should work :)

karan.pathak gravatar image karan.pathak  ( 2020-12-16 16:41:02 -0500 )edit