SOS! libtorch conflict with ROS!

I have a project using libtorch and we need wrapper it into a ros package. nightmare comes:

we have no problem to build executable program without ros. But when using is along with catkin:

set(CMAKE_PREFIX_PATH $ENV{HOME}/libtorch)
find_package(Torch REQUIRED)
include_directories(include/)
#include_directories("$ENV{HOME}/libtorch/include/")
#include_directories("$ENV{HOME}/libtorch/include/torch/csrc/api/include")
if (NOT Torch_FOUND)
    message(FATAL_ERROR "libtorch not found, wavehands_stop will not built.")
else ()
    message(STATUS "++++ found libtorch ")

It will wipe out catkin libs and throw out undefined ros::inti blabla errors, I dont know why.

And even torch was found, it still can not found torch/torch.h header file:

image

I am currently have some thoughts on this but I can not solve the problem, kindly want community give me some help!!

  • it was caused by libtorch CMAKE_PREFIX_PATH I think, this variable can not get along with catkin, since found torch need this, I can not delete it;
  • I can not manually set header path point to torch/torch.h, it cause a lot of warnings and even undefined errors even I link TORCH_LIBRARIES.

Here is my cmakelists in ROS:

cmake_minimum_required(VERSION 2.8.3)
project(ws_stop)


add_definitions("-std=c++11")

find_package(catkin REQUIRED COMPONENTS
        roscpp
        pcl_conversions
        pcl_ros
        sensor_msgs
        geometry_msgs
        message_filters
        tf_conversions
        tf
        laser_geometry
        cv_bridge
        std_msgs
        message_generation)

find_package(OpenCV REQUIRED)
find_package(Eigen3 REQUIRED)

catkin_package(
)

include_directories(
        include
        ${catkin_INCLUDE_DIRS}
        ${EIGEN3_INCLUDE_DIR}
        ${OpenCV_INCLUDE_DIRS}
        #        ${PCL_INCLUDE_DIRS}
)




# !---------------] find libs what wavehands need
set(CMAKE_PREFIX_PATH $ENV{HOME}/libtorch)
find_package(Torch REQUIRED)
include_directories(include/)
#include_directories("$ENV{HOME}/libtorch/include/")
#include_directories("$ENV{HOME}/libtorch/include/torch/csrc/api/include")
if (NOT Torch_FOUND)
    message(FATAL_ERROR "libtorch not found,  will not built.")
else ()
    message(STATUS "++++ found libtorch ")

    find_library(HUMAN_POSE_ESTIMATION
            NAMES human_pose_estimation
            HINTS ./libs
            PATHS ./libs)
    file(GLOB_RECURSE
            SOURCES
            src/*.cc)

    add_executable(w_stop_node ${SOURCES})
    target_link_libraries(
            wavehands_stop_node
            ${catkin_LIBRARIES}
#            ${TORCH_LIBRARIES}
#            torch
#            c10
#            ${HUMAN_POSE_ESTIMATION}
    )
    get_target_property(TARGET_LIBRARIES w_stop_node LINK_LIBRARIES)
    message(STATUS ${TARGET_LIBRARIES})
endif ()

Any one could give a indicates or sample cmakelists which can link libtorch correctly in ros??

did you build libtorch from source?

No, I using prebuilt binaries.

I found the root reason is that, find torch will add a flag which is:

#set(CMAKE_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")

Once this flag added, the ros can not built since ros relate lib will not link.

if disbale it , the libtorch will not link. error like:

undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

I am trying to built libtorch from scratch

But how to built libtorch with the new ABI? which flags should I change ??

I have the same problem, Did you solve the problem?

We are still working on prebuilt libtorch binary with D_GLIBCXX_USE_CXX11_ABI=1, but currently hitting some roadblocks. Progress is tracked at https://github.com/pytorch/pytorch/issues/17492.

Yes! I solved it by built from source code by using modern ABI

Hi! Why are u saying working on new ABI…

Does it simple? I built it from source and solved my problem…

Would you mind sending a binary file to me? or push it to github, I have followed you

This problem is caused by ABI incompatibility. Particularly, the libtorch binaries from PyTorch website are built using the old ABI, which is not binary compatible with ROS libraries in this case.

If you build libtorch from source using the new ABI, then it’s binary compatible with ROS libraries and there won’t be any linking error.

We now have CXX11 ABI binaries for libtorch. They can be found on
http://pytorch.org, or at https://github.com/pytorch/pytorch/issues/17492#issuecomment-524692441.

1 Like

Do you share the scripts which you use to build libtorch from source ? Thanks