Help building cpp code with opencv

username: ash_gamma
cooked: <p>I run into the following error when I try to compile a modified version of this project: <a href="https://github.com/Wizaron/pytorch-cpp-inference" class="inline-onebox" rel="noopener nofollow ugc">GitHub - Wizaron/pytorch-cpp-inference: Serving PyTorch 1.0 Models as a Web Server in C++</a></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/4/4771d0bdb727a1a808095031718d0d50a7aadafc.png" data-download-href="https://discuss.pytorch.org/uploads/default/4771d0bdb727a1a808095031718d0d50a7aadafc" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/4/4771d0bdb727a1a808095031718d0d50a7aadafc.png" alt="image" data-base62-sha1="ac1KqeDt1Ai5zKq9Sy8GTVnkwNK" width="690" height="238" data-dominant-color="151615"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1277×442 26.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I’m using the unzipped libtorch from Pytorch’s website. And here’s my CMakeLists.txt</p>
<pre><code class="lang-auto">make_minimum_required(VERSION 3.0 FATAL_ERROR)
project(cnn-classification)

find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)

add_executable(cnn-inference main.cc infer.cc ../../utils/opencvutils.cc ../../utils/torchutils.cc)

target_link_libraries(cnn-inference "${TORCH_LIBRARIES}")
target_link_libraries(cnn-inference "${OpenCV_LIBS}")

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


set_property(TARGET cnn-inference PROPERTY CXX_STANDARD 11)
#set_property(TARGET cnn-inference PROPERTY OUTPUT_NAME predict)

set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
</code></pre>
<p>Could someone help me with these linkage errors? Do I need to compile libtorch from source?</p>
<p>Thanks! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

username: yf225
cooked: <p>This can be due to ABI-incompatibility issue. You might want to try compiling libtorch from source, and make sure you are using the same compiler to compile both opencv and libtorch.</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>. It should fix the problem.</p>

username: dambo
cooked: <p>See my response here:</p><aside class="quote" data-post="10" data-topic="39490">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dambo/48/29088_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/libtorch-with-new-c-11-abi/39490/10">Libtorch with new C++11 ABI?</a> <a class="badge-category__wrapper " href="/c/c/11"><span data-category-id="11" data-drop-close="true" class="badge-category " title="Topics related to the C++ Frontend, C++ API or C++ Extensions"><span class="badge-category__name">C++</span></span></a>
  </div>
  <blockquote>
    I have integrated the new ABI lib in my Docker file which also encapsulates the correct OpenCV version so that the notorious cv::imread() method works. I worked days and nights on attempting to compile libtorch + OpenCV from scratch with or without the -DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=0 flag.
  </blockquote>
</aside>
<p>
I used a pre-compiled OpenCV version. This is the only solution that worked for me.</p>