Libtorch does not link together with boost

username: HariSeldon
cooked: <p>Hi all,</p>
<p>I would like to report a build issue when linking an application that uses both libtorch and boost.</p>
<p>tensor_test.cpp:</p>
<pre><code class="lang-auto">#include &lt;boost/program_options/options_description.hpp&gt;

int main() {
    boost::program_options::options_description d("");
}
</code></pre>
<p>CMakeLists.txt:</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(torch_example)

set(Torch_DIR "${CMAKE_SOURCE_DIR}/libtorch/share/cmake/Torch")
set(Caffe2_DIR "${CMAKE_SOURCE_DIR}/libtorch/share/cmake/Caffe2")
find_package(Torch REQUIRED)

message(STATUS ${TORCH_LIBRARIES})

find_package(Boost COMPONENTS program_options REQUIRED)

add_executable(main tensor_test.cpp)
target_link_libraries(main Boost::program_options ${TORCH_LIBRARIES})

set_property(TARGET main PROPERTY CXX_STANDARD 11)
</code></pre>
<p>This is the link error I get:</p>
<pre><code class="lang-auto">Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/tensor_test.cpp.o
[100%] Linking CXX executable main
CMakeFiles/main.dir/tensor_test.cpp.o: In function `main':
tensor_test.cpp:(.text+0x6b): undefined reference to `boost::program_options::options_description::options_description(std::string const&amp;, unsigned int, unsigned int)'
</code></pre>
<p>When removing the ${TORCH_LIBRARIES} variable from target_link_libraries linking works correctly.</p>
<p>The same issue arises with other shared libraries.</p>
<p>I guess this is a similar issue to the one reported here:</p><aside class="quote quote-modified" data-post="1" data-topic="27114">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/e95f7d/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/compile-libtorch-with-pcl-cause-segmentation-fault/27114">Compile libtorch with pcl cause segmentation fault</a> 
  </div>
  <blockquote>
    I try to compile libtorch with PCL library. Both the libraries work fine when compiled dependently, but segmentation fault occurs when complie these libraries together. 

cmake file:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(custom_ops)
SET(CMAKE_BUILD_TYPE "Debug")

list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/libtorch)

find_package(Torch REQUIRED)
find_package(PCL 1.8 REQUIRED COMPONENTS common io visualization)

include_directories(
        ${PCL_INCLUDE_DIRS}
)

…
  </blockquote>
</aside>

<p>I am on Ubuntu18.04 and I am using libtorch 1.0.0.dev20181022, boost 1.65.1</p>

username: albanD
cooked: <p>Hi,</p>
<p>I just used your two files and that compiles fine for me on Ubuntu 14.04.<br>
Maybe try including them by two different calls?</p>
<pre><code class="lang-auto">target_link_libraries(main Boost::program_options)
target_link_libraries(main ${TORCH_LIBRARIES})
</code></pre>

username: HariSeldon
cooked: <p>Hi,</p>
<p>thank you for trying it out.<br>
The problem seems related to the Ubuntu version.<br>
I confirm that the build works with 14.04, but it fails on Ubuntu 16.04 and 18.04.</p>
<p>I particular, I moved the object file from the working 14.04 machine to the 16.04 machine and the link stage failed.<br>
I have used g++ 5.5.0 20171010 on both.</p>
<p>(The suggestion of duplicating the target_link_libraries function does not make a difference)</p>

username: HariSeldon
cooked: <p>Just for the record,<br>
I have worked around the problem by compiling libtorch locally on my machine from<br>
source.</p>

username: KevNull
cooked: <p>Does anybody know how to fix this problem? I’m finding the same thing happens with another proprietary library.</p>

username: KevNull
cooked: <p>Could this be caused by the nightly build coming with an absolute file path to /usr/local/cuda/lib64/libculibos.a in Caffe2Targets.cmake?</p>

username: christianperone
cooked: <p>I think that this can be related to the C++11 ABI as well, see this comment I posted: <a href="https://discuss.pytorch.org/t/issues-linking-with-libtorch-c-11-abi/29510/4?u=christianperone">Issues linking with libtorch (C++11 ABI?)</a>, I’m facing similar issues with other libraries. The issue seems that libtorch is adding this definition to compilation <code>_GLIBCXX_USE_CXX11_ABI=0</code>, which forces GCC to use old C++11 ABI, and this is incompatible with your boost dependency, hence the error you’re getting, because the <code>std::string</code> was changed between old and new C++11 ABI.</p>

username: Oldpan
cooked: <p>Hi,there is a similar problem …<br>
When libtorch is built with opencv :</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(simnet)

find_package(Torch REQUIRED)
find_package(OpenCV 4 REQUIRED)

message(STATUS "Pytorch status:")
message(STATUS "    libraries: ${TORCH_LIBRARIES}")

message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

add_executable(simnet main.cpp)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

target_link_libraries(simnet "${TORCH_LIBRARIES}")
target_link_libraries(simnet "${OpenCV_LIBS}")
</code></pre>
<p>It will give rise to ‘undefined reference to `cv::imread(std::string const&amp;, int)’’<br>
And when I removed ‘target_link_libraries(simnet “${TORCH_LIBRARIES}”)’ and the build process succeeded.</p>

username: nallezard
cooked: <p>Hi, I have the  same problem with opencv and libtorch<br>
I’ve also tried this piece of code : <a href="https://github.com/tobiascz/MNIST_Pytorch_python_and_capi" rel="nofollow noopener">https://github.com/tobiascz/MNIST_Pytorch_python_and_capi</a><br>
But it does’nt work either…<br>
Has anyone found the solution?<br>
Thanks</p>

username: KevNull
cooked: <p>I was able to fix it by building from source instead of trying to use the nightly builds.</p>

username: nallezard
cooked: <p>Thanks for the reply KevNull<br>
I’ve just build pytorch from source now and i search how to link it with my code.</p>

username: yf225
cooked: <p>We now have new 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>