SOS! libtorch conflict with ROS!

jinfagang: <p>I have a project using libtorch and we need wrapper it into a ros package. nightmare comes:</p>
<p>we have no problem to build executable program without ros. But when using is along with catkin:</p>
<pre><code class="lang-auto">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 ")
</code></pre>
<p>It will wipe out catkin libs and throw out undefined ros::inti blabla errors, I dont know why.</p>
<p>And even torch was found, it still can not found  torch/torch.h header file:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/e/edb2afd40b398bc5256e85ef0029186490116272.png" alt="image" data-base62-sha1="xULZHMVOCgwgdPzU6nogX0Aw1CW" width="451" height="168"></p>
<p>I am currently have some thoughts on this but I can not solve the problem, kindly want community give me some help!!</p>
<ul>
<li>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;</li>
<li>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.</li>
</ul>
<p>Here is my cmakelists in ROS:</p>
<pre><code class="lang-auto">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 ()

</code></pre>
<p>Any one could give a indicates or sample cmakelists which can link libtorch correctly in ros??</p>

mhubii: <p>did you build libtorch from source?</p>

jinfagang: <p>No, I using prebuilt binaries.</p>
<p>I found the root reason is that, find torch will add a flag which is:</p>
<pre><code class="lang-auto">#set(CMAKE_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")
</code></pre>
<p>Once this flag added, the ros can not built since ros relate lib will not link.</p>
<p>if disbale it , the libtorch will not link. error like:</p>
<pre><code class="lang-auto">undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'

</code></pre>
<p>I am trying to built libtorch from scratch</p>
<p>But how to built libtorch with the new ABI? which flags should I change ??</p>

Yuliang_Tang: <p>I have the same problem, Did you solve the problem?</p>

yf225: <p>We are still working on prebuilt libtorch binary with <code>D_GLIBCXX_USE_CXX11_ABI=1</code>, but currently hitting some roadblocks. Progress is tracked at <a href="https://github.com/pytorch/pytorch/issues/17492" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/17492</a>.</p>

jinfagang: <p>Yes! I solved it by built from source code by using modern ABI</p>

jinfagang: <p>Hi! Why are u saying working on new ABI…</p>
<p>Does it simple? I built it from source and solved my problem…</p>

Yuliang_Tang: <p>Would you mind sending a binary file to me? or push it to github, I have followed you</p>

yf225: <p>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.</p>
<p>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.</p>

yf225: <p>We now have CXX11 ABI binaries for libtorch. They can be found on<br>
<a href="http://pytorch.org/" rel="nofollow noopener">http://pytorch.org</a>, or at <a href="https://github.com/pytorch/pytorch/issues/17492#issuecomment-524692441" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/17492#issuecomment-524692441</a>.</p>

dat_pham_thanh: <p>Do you share the scripts which you use to build libtorch from source ? Thanks</p>